Home > Net >  Can't find elements but they exist Selenium Python
Can't find elements but they exist Selenium Python

Time:07-12

I have encountered a problem: the program does not see the elements and cannot print them. Though, I use it several times in code and it worked the first time. A little later, as I said, it stops seeing the element:

HTML code:

<div >
 <div >...</div>
 <div >...</div>
</div>

Python code:

elements = driver.find_elements(By.XPATH, "//div[@class='entries-container']/div")
print(len(elements))
print(driver.find_elements(By.XPATH, "//div[@class='entries-container']/div"))

Console output:

0
[]

If instead of elements to find 1 element (print(driver.find_element(By.XPATH, "//div[@class='entries-container']/div"))), then an error will appear in the console, Unable to locale element

What is the problem? Is there another way to find an element?

Updated: I just recreated the HTML page and the program started working. Most likely it was a bug.

CodePudding user response:

To get string value from the text nodes you have to invoke .text method. The following xpath expression selects all the div elements

elements = driver.find_elements(By.XPATH, "//div[@class='entries-container']/div")
for element in elements:
    print(element.text)

See the provement from here

  • Related