【问题标题】:Finding element with explicit wait using selenium webdriver in python在 python 中使用 selenium webdriver 查找具有显式等待的元素
【发布时间】:2014-11-09 03:28:22
【问题描述】:

我正在尝试使用链接文本查找元素,我正在使用以下代码

handle = driver.window_handles
#handle for windows
driver.switch_to.window(handle[1])
#switching to new window
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))

我正在跟踪回溯

Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
raise TimeoutException(message)
TimeoutException: Message: ''

我要选择的元素的 HTML 是

<a href="/Kevin-Rose/followers">Followers <span class="profile_count">43,799</span></a>

我该如何解决这个问题??

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver


    【解决方案1】:

    如果您使用By.LINK_TEXT,则应该有一个与该文本完全相同的链接:Followers,但您有Followers 43,799

    在您的情况下,您应该改用By.PARTIAL_LINK_TEXT

    wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Followers')))
    

    更新以下是工作示例:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()  # CHANGEME
    driver.get('http://www.quora.com/Kevin-Rose')
    element = WebDriverWait(driver, 2).until(
        EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Followers"))
    )
    element.click()
    

    【讨论】:

    • 它给出了同样的错误。我尝试使用 ids 和 classes 查找不同的元素,它给出了相同的错误。我还下载了页面的 html 源代码,以确保我在同一页面上。这是“quora.com/Kevin-Rose”页面的链接,我正在尝试点击关注者链接。
    • @Siddhesh,我添加了一个工作示例。问题是尾随空格。请检查一下。
    • 如何知道By. 之后存在哪些论据?我很难找到这方面的文档..
    • 非常感谢!那很快:)
    • 对于任何想知道如何找到作者的人。选项。如果您有 PyCharm 或类似的东西,请在脚本顶部导入包并按字面意思写 By。您将看到所有选项都出现在下拉列表中。我知道这非常简单明了,但我在懒得检查之前就去谷歌搜索了。
    【解决方案2】:

    干杯

    from selenium import webdrivercode
    from selenium.webdriver.common.by import By
    import selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get("http://somedomain/url_that_delays_loading")
    
    #Follow below syntax
    element = WebDriverWait(driver, 10)
    element.until(EC.presence_of_element_located((By.ID, '--webElement--')))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      相关资源
      最近更新 更多