简体   繁体   English

找不到元素但它们存在 Selenium Python

[英]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: HTML 代码:

<div class="entries-container">
 <div class="row step finished">...</div>
 <div class="row step finished">...</div>
</div>

Python code: Python代码:

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如果不是元素来查找1个元素( print(driver.find_element(By.XPATH, "//div[@class='entries-container']/div")) ),那么控制台会出现错误,无法到语言环境元素

What is the problem?问题是什么? Is there another way to find an element?还有其他方法可以找到元素吗?

Updated: I just recreated the HTML page and the program started working.更新:我刚刚重新创建了 HTML 页面,程序开始工作。 Most likely it was a bug.很可能这是一个错误。

To get string value from the text nodes you have to invoke .text method.要从文本节点获取字符串值,您必须调用.text方法。 The following xpath expression selects all the div elements以下 xpath 表达式选择所有 div 元素

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

See the provement from here这里查看证明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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