简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM