【问题标题】:Python, selenium - how to refresh page until item found?Python,硒 - 如何刷新页面直到找到项目?
【发布时间】:2018-08-16 00:22:19
【问题描述】:

我正在尝试刷新页面,直到项目出现,但我的代码不起作用(我对此采取了模式:python selenium keep refreshing until item found (Chromedriver))。

代码如下:

while True:
    try:
        for h1 in driver.find_elements_by_class_name("name-link"):
            text = h1.text.replace('\uFEFF', "")
            if "Puffy" in text:
                break
    except NoSuchElementException:
        driver.refresh
    else:
        for h1 in driver.find_elements_by_class_name("name-link"):
            text = h1.text.replace('\uFEFF', "")
            if "Puffy" in text:
                h1.click()
                break
        break

这些片段是因为我必须找到一个具有相同类名的项目并将 BOM 替换为“”(find_element_by_partial_link_text 不起作用)。

for h1 in driver.find_elements_by_class_name("name-link"):
    text = h1.text.replace('\uFEFF', "")
    if "Puffy" in text:
        break

有人可以帮我吗?非常感谢。

【问题讨论】:

  • 为什么页面上没有项目?为什么刷新会让它们出现?

标签: python html selenium refresh zero-width-space


【解决方案1】:

您正在尝试获取元素列表(driver.find_elements_by_class_name() 可能返回元素列表或空列表 - 没有例外) - 在这种情况下您无法获取 NoSuchElementException,因此不会执行 driver.refresh。请改用下面的方法

while True:
    if any(["Puffy" in h1.text.replace('\uFEFF', "") for h1 in driver.find_elements_by_class_name("name-link")]):
        break
    else:
        driver.refresh
driver.find_element_by_xpath("//*[contains(., 'Puffy')]").click()

【讨论】:

  • 但是我可以通过什么方式点击这些元素呢?
  • 哦,我没注意到你也想点击它...试试driver.find_element_by_xpath("//*[contains(., 'Puffy')]").click()
  • 但是,我不需要这个(点击),但你的答案有效。谢谢!
猜你喜欢
  • 2017-07-29
  • 1970-01-01
  • 2021-08-13
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 2012-12-19
  • 2022-07-31
相关资源
最近更新 更多