【问题标题】:len(browser.find_elements_by_class_name('foo')) gives 0len(browser.find_elements_by_class_name('foo')) 给出 0
【发布时间】:2020-10-28 19:43:51
【问题描述】:

我正在使用 python 3.7 并且在 selenium 中遇到错误

我有一个类似的网站:

<html>
    <body>
        <div class="foo">
            <div class="bar1">something clickable</div>
            <div class="bar2">something clickable</div>
            <div class="bar3">something clickable</div>
            <div class="bar4">something clickable</div>
    </div>
<body>
<html>

我想使用 selenium 来点击每个条,所以我写道:

from selenium import webdriver
browser=webdriver.Chrome()
browser.get('https://www.some-website.com')
for i in browser.find_elements_by_class_name('foo'):
    i.click()

但是,执行时我什么也没得到,所以我运行:

print(len(browser.find_elements_by_class_name('foo')))

输出为零

即使我在得到零之前输入WebDriverWait...

为什么?

【问题讨论】:

    标签: python html python-3.x selenium selenium-webdriver


    【解决方案1】:

    试试这个 xpath,这将返回 4 个 webelements

    driver.find_elements_by_xpath("//div[@class='foo']//*")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多