-2

Here is my HTML and trying to get the text must be unique. with Python 2.7

my image html

Tried to use:

driver.find_element_by_xpath("//span[@id='lblError']/text()")

The xpath was not valid one.

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
Ari N
  • 117
  • 1
  • 1
  • 13

3 Answers3

0

try to find the element by ID then get_attribute("innerHTML").

var el = driver.find_element_by_id("lblerror")
var text = el.get_attribute("innerHTML")
Ywapom
  • 601
  • 5
  • 18
  • I am using python, above code seems to be for java. I have tried to use: text = driver.find_element_by_id("lblError") and that error out. – Ari N Jan 17 '19 at 22:47
  • first, your snippet is wrong. look in the python webdriver docs https://selenium-python.readthedocs.io/ – Ywapom Jan 17 '19 at 23:09
  • Thank you for reference link but my snippet is not wrong. I will attach a whole thing. – Ari N Jan 17 '19 at 23:21
  • I didn't write " text = driver.find_element_by_id("lblError")" which you show as an error – Ywapom Jan 17 '19 at 23:22
0

To retrieve the text must be unique you can use the execute_script() method and you can use the following solution:

myElement = driver.find_element_by_xpath("//span[@id='lblError']")
myText = driver.execute_script('return arguments[0].lastChild.textContent;', myElement).strip()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The below did not work, it seems to be invalid path :myElement = driver.find_element_by_xpath("//span[@id='lblError']") – Ari N Jan 18 '19 at 22:15
  • Hmm, _invalid path_, I took the reference from your code trials and the snapshot of the HTML you have provided. Can you update the question with the text based relevant HTML? As you are using **pyhton 2.7** remember to append **self** before the applicable one. – undetected Selenium Jan 18 '19 at 22:24
-1

enter code heretime.sleep(1) enter code hereelement = driver.find_element_by_xpath("//span[@id='lblError']") enter code heretext = element.text

Ari N
  • 117
  • 1
  • 1
  • 13