Python – selenium python xpath element text not found on page ()

selenium python xpath element text not found on page ()… here is a solution to the problem.

selenium python xpath element text not found on page ()

I’m trying to validate the text Active:

<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 the TEXT to make sure it’s there. I use the following method to find TEXT:

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

When I print(foo.text), I can see all the text in the html example above. So, I guess I can 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 also tried:

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

Still nothing. Any help would be appreciated.

Solution

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

  • Print TEXT:

    print(b.find_element_by_xpath("//div[@class='details']//span[@class='bold']").get_attribute("innerHTML"))
    
  • 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])
    
  • Print GAME:

    print(b.find_element_by_xpath("//div[@class='details']//a[@class='game']").get_attribute("innerHTML"))
    

Related Problems and Solutions