3

I'm trying to verify the text Active: from this example

<div class="details">
    <p class="double">
        <a href="url">Set as default</a>
        <br>
        <a id="modal124434" class="modalAction" href="#">Delete</a>
    </p>
    <span class="bold">TEXT</span>
    : 1234567890 (12/2017)
    <br>
    Active:
    <a class="game" href="#GAME">GAME</a>
</div>

I also need to check TEXT to make sure it's there. I used the following to find TEXT:

foo = b.find_element_by_xpath('//span[contains(text(),"TEXT")]/..')

and when I print(foo.text) I can see all the text from the html example abve. So, I thought I could do something like this:

b.find_element_by_xpath('//span[contains(text(),"TEXT")]/..[contains(text(),"Active:")]/a[text()="GAME"]

but I get a NoSuchElement exception. I've also tried:

b.find_element_by_xpath('//*[contains(text(),"Active")]')

and still got nothing. Any help would be much appreciated.

7
  • In this example, the text you are after is between two elements. Perhaps the answer here will be helpful: stackoverflow.com/questions/15813903/… Commented Dec 20, 2017 at 1:27
  • Which text are you trying to locate exactly? TEXT or Active: or GAME? Commented Dec 20, 2017 at 5:37
  • Do you need to get substring "Active:" from node or you need to locate element by substrings "TEXT" and "Active:"? Commented Dec 20, 2017 at 8:17
  • @debanjanbI and @Andersson was hoping to verify the text of all three in one xpath if possible. @Tim So once I've found the text()[2] how can I tell what text is in that element? Commented Dec 20, 2017 at 18:55
  • So it turns out for what I'm doing I don't actually need to verify the Active: text, because the <a> that has GAME in it will not appear if Active: isn't present. So I was able to use '//span[contains(.,"TEXT")]/../a[text()="GAME"]' Commented Dec 20, 2017 at 21:06

3 Answers 3

1

To print the element texts TEXT, Active: and GAME you can use the following code blocks :

  • To print TEXT :

    print(b.find_element_by_xpath("//div[@class='details']//span[@class='bold']").get_attribute("innerHTML"))
    
  • To print Active: :

    fullsting = b.find_element_by_xpath("//div[@class='details']").get_attribute("innerHTML")
    part_word = fullsting.split(")")
    words = part_word[1].split("G")
    print(words[0])
    
  • To print GAME :

    print(b.find_element_by_xpath("//div[@class='details']//a[@class='game']").get_attribute("innerHTML"))
    
Sign up to request clarification or add additional context in comments.

4 Comments

The innerHTML attribute is cool, I did not know about that, but I was hoping to use one xpath string to verify all three of these items at once.
@DanielWillemin Nice to know that. Did your Question get answered?
Sadly no, I did find a work around not involving the text Active: though.
Coming back to this, I have figured out a way to do what I originally wanted. I've posted my answer.
1

You can try this xpath :- //span/following-sibling::text()[2]

driver.find_element_by_xpath("//span/following-sibling::text()[2]").text ;

3 Comments

once I've found the text()[2] how can I tell what text is in that element?
I get an exception when trying this selenium.common.exceptions.WebDriverException: Message: TypeError: Argument 1 of Window.getComputedStyle does not implement interface Element.
I ended up using part of your answer for the work around that I used for this. I recently realized that I never posted my work around. I used the following xpath: '//span[text()="TEXT"]/following-sibling::a[text()="GAME"]'
1

Coming back to this I was able to find a way to do what I originally wanted

b.find_element_by_xpath('//span[contains(text(),"TEXT")]/..[contains(.,"Active:")]/a[text()="GAME"]')

it seems like using text() only looks into the first textNode that appears on that web element. but using . looks at all textNodes of that element as well as any textNodes that are attached to any childNode "beneath" the current element.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.