Home > Back-end >  driver.find_element_by_xpath - trying to retreive the text
driver.find_element_by_xpath - trying to retreive the text

Time:09-21

Here is my element in an html page

<div class='test'>my text</div>

I'm trying to retrieve the text of this div with my selenium driver in python

mytext = driver.find_element_by_xpath("//div[contains(@class, 'test')/text()]")
print mytext

but nothing is returned

Thank you

this returns "True" :
try:
        driver.find_element_by_xpath("//div[@class='test']")
        mystatus = "True"
except NoSuchElementException:
        mystatus = "False"
    

but this returns "False"

try:
        driver.find_element_by_xpath("//div[@class='test']/text()")
        mystatus = "True"
except NoSuchElementException:
        mystatus = "False"

CodePudding user response:

Try getting the text after you have the element with .text in Python.

driver.find_element_by_xpath("//div[@class='test']").text

CodePudding user response:

Slight adjustment:

//div[contains(@class, 'test')]/text()

You just had the closing square bracket in the wrong place.

  • Related