-1

I am trying to select I NEED THIS TEXT from the last line of the following html code and did not yet have success so far:

        <div class="warn">
<div class="row container pv2">
    <div class="col xs12">
        <div class="display-table-cell b-text_copy-4 pr1">
            <i class="msg-icon b-icon b-icon-warn"></i>
        </div>
        <div class="display-table-cell b-text_copy-2 b-text_weight-bold">
                    <div> I NEED THIS TEXT <a href=https://somelink/contact.html target=_blank>contact us</a>.</div>

INPUT I failed with the following approaches:

# all lines also tested without .text suffix
    text1 = driver.find_element_by_class_name("display-table-cell.b-text_copy-2.
                                              b-text_weight-bold").text
    text2 = driver.find_element_by_class_name("warn").text 
    text3 = driver.find_element_by_class_name("col.xs12").text
    text4 = driver.find_element_by_class_name("display-table-cell").text
    text5 = driver.find_element_by_xpath("//*[contains(@text='I NEED THIS TEXT')]")

OUTPUT:

text1: Message: Unable to locate element: .display-table-cell.b-text_copy-2.b-text_weight-bold
text2: Message: Unable to locate element: .warn
text3: Message: Unable to locate element: .col.xs12
text4: Message: Unable to locate element: .display-table-cell
text5: Message: Given xpath expression "//*[contains(@text='I NEED THIS TEXT')]" is invalid: [Exception... "<no message>"  nsresult: "0x8060000d (<unknown>)"  location: "JS frame :: chrome://marionette/content/element.js :: element.findByXPath :: line 355"  data: no]

Am I doing something fundamentally wrong? How to select I NEED THIS TEXT and print it to console?

sudonym
  • 3,788
  • 4
  • 36
  • 61

2 Answers2

0

Our main aim was to extract the text I NEED THIS TEXT from the following node :

<div> I NEED THIS TEXT <a href=https://somelink/contact.html target=_blank>contact us</a>.</div>

Now as per your code trials you have tried to use the class_name display-table-cell.b-text_copy-2.b-text_weight-bold, warn, col.xs12, display-table-cell and xpath as //*[contains(@text='I NEED THIS TEXT')]. Through those class_name you haven't reached to the exact Node which contains the intended text. Hence we have to construct either an xpath or a cssSelector to mock the DOM and reach till the intended <div> tag where the text is located.

So, to print I NEED THIS TEXT to console from the last line of the given html you can use the following code block :

my_string = driver.find_element_by_xpath("//div[ancestor::div[contains(@class, 'display-table-cell b-text_copy-2 b-text_weight-bold')]]").get_attribute("innerHTML")                    
my_text = my_string.split("contact")
print(my_text[0])

Console Output :

I NEED THIS TEXT 
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • returns Error:Message: Given xpath expression "//div[@class='display-table-cell b-text_copy-2 b-text_weight-bold']/[(self::div) and not(@href='https://somelink/contact.html')]" is invalid: SyntaxError: The expression is not a legal expression. – sudonym Dec 19 '17 at 04:46
  • Why use innerHTML? You will get " I NEED THIS TEXT contact us.". – JeffC Dec 19 '17 at 05:07
  • @sudonym Check out my updated Answer and let me know the status. – undetected Selenium Dec 19 '17 at 14:16
  • @JeffC I think you are pretty much confused and hesitant about using [**`get_attribute`**](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html) and [**`innerHTML`**](https://www.w3schools.com/jsref/prop_html_innerhtml.asp). Please go through the docs and let me know if you have any Questions. – undetected Selenium Dec 19 '17 at 14:27
  • I understand exactly what it will do ... that's why I printed it in my comment. Was I incorrect? If so, what should the output have been? You seem to be the one that is confused. That's clearly why you changed your answer. – JeffC Dec 19 '17 at 16:05
0

The main problem is that you are using mostly CSS selectors instead of class names. .find_element_by_class_name() is specifically for a single class name. If you want to use a CSS selector you need to use .find_element_by_css_selector(). Most of the CSS selectors you were using weren't actually for the DIV that you want either. It's hard to tell without a link to the page or more HTML but try these.

CSS Selector

div.display-table-cell.b-text_copy-2.b-text_weight-bold > div

XPath

//div[contains(.,'I NEED THIS TEXT')]
//a[.='contact us']/..

One issue you are going to run into is that the DIV that immediately contains the text you want also contains " contact us." so you will need to strip that out if you don't want it as part of the final string.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • you are perfectly right - do you mind me contacting you via email? – sudonym Dec 19 '17 at 06:09
  • 2
    You have spoken about what OP have already tried and failed. You answer doesn't contain any split logic to extract the text which the OP needs. – undetected Selenium Dec 19 '17 at 14:27
  • @DebanjanB Yep... I believe that's the core of a good answer. To explain why the OP's attempts failed so they can learn what they were doing wrong. Another crucial part of a good answer is to actually answer the question. I've done that also by providing 3 different approaches. I addressed the part about the extra text being printed but OP didn't state whether they cared if that text was in there so I described how to fix it themselves which is covered in many, many other questions and blogs and tutorials on the web so it was left to OP to figure out. – JeffC Dec 19 '17 at 16:09
  • @DebanjanB What are you doing is called revenge downvoting. You downvoted me because I downvoted you. I've noticed you doing it before and you need to stop. I downvoted your answer for a legitimate reason and explained why in my comment. If you can prove me wrong, I'll reverse my downvote. Your comment on my answer had no content. You'll notice the OP agreed with me and left no comment about corrections to my answer. Read: https://meta.stackexchange.com/questions/106704/how-should-i-handle-possible-revenge-downvotes. – JeffC Dec 19 '17 at 16:16
  • @sudonym I don't give out my personal email. You can leave comments here and I'll see them and generally respond. – JeffC Dec 19 '17 at 16:17