繁体   English   中英

在继续使用Selenium和python之前如何检查元素是否存在?

[英]How to check if element is present before continuing using selenium and python?

只要网站上存在特定的div元素,我将如何运行循环?

我需要解析器浏览多个页面,然后为每个页面单击下一步 -最后一页是空的,因此缺少上述div元素,因此我认为在while循环中运行此页面将是一种不错的方法。 但是我不确定如何正确地做到这一点。

我目前所拥有的(尽管无法运行)是:

test = driver.find_element_by_class_name("divClassName").size()

,然后检查其大小是否为while(test != 0)

我们欢迎所有的建议!

尝试实施以下方法:

from selenium.common.exceptions import NoSuchElementException    

while True:
    try:
        driver.find_element_by_class_name("divClassName")
        # do what you need
        # click "Next" button
    except NoSuchElementException:
        break

使用上面的代码,如果找到<div class="divClassName"> ,您将能够获取新页面并执行操作,否则停止

还要注意, size是webelement的property ,而不是method 您应该将其用作webelement.size而不是webelement.size()

暂无
暂无

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

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