KraKoff_
KraKoff_

Reputation: 37

Can't find elements but they exist Selenium Python

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 class="entries-container">
 <div class="row step finished">...</div>
 <div class="row step finished">...</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.

Upvotes: 0

Views: 84

Answers (1)

Md. Fazlul Hoque
Md. Fazlul Hoque

Reputation: 16187

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

Upvotes: 1

Related Questions