【问题标题】:python selenium keep refreshing until item found (Chromedriver)python selenium 保持刷新直到找到项目(Chromedriver)
【发布时间】:2017-07-29 18:00:37
【问题描述】:

我正在尝试使用 selenium 来实现 Python 脚本的功能,以不断刷新当前的 chromepage,直到找到创建 driver.find_element_by_partial_link_text("Schott") 的某个项目。

我在想这个:

while not driver.find_element_by_partial_link_text("Schott"):
    driver.refresh
driver.find_element_by_partial_link_text("Schott").click()

但是,driver.find_element_by_partial_link_text("Schott") 函数似乎无法满足需求。请问还有其他方法可以实现吗?

顺便说一句,目前我正在使用 driver.get(url) 打开网页,但我想知道如何在我已经打开的现有网页上运行脚本?

【问题讨论】:

    标签: python html google-chrome selenium selenium-chromedriver


    【解决方案1】:

    如果找不到元素,使用find_element_... 将引发NoSuchElementExeception。由于我不知道您针对的是哪个站点,因此我不知道最佳实践是什么。但是,如果它只是刷新页面以检查元素,您可以尝试以下操作:

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    
    driver.Firefox()  # or whatever webdriver you're using
    driver.get(url that you are going to)
    while True:
        try:
            driver.find_element_by_partial_link_text("Schott"):
        except NoSuchElementException:
            driver.refresh
        else:
            driver.find_element_by_partial_link_text("Schott").click()
            break
    

    【讨论】:

    • 另外,有没有办法在同一个 chrome 页面下打开新标签?喜欢“Control + T”命令然后在新标签上做一些类似的操作?
    • @EdisonHua 你用的是什么浏览器?编辑:没关系......你的标签说铬。给我一秒钟
    • 要在 chrome 中打开一个新选项卡,我能够让它工作的唯一方法是使用 driver.execute_script('window.open("about:blank", "_blank");') 但你必须使用 @987654328 将焦点更改为 window_handle @
    • 通过window.open,如果我想打开google.com,我该怎么办?我试过 driver.execute_script('window.open("google.com");') 但失败了:(
    • @EdisonHua 包含完整的 url,包括 http://....driver.execute_script('window.open("http://www.google.com", "_blank");')
    猜你喜欢
    • 2018-08-16
    • 2022-01-10
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多