python - 如何在使用 Selenium 和 Python 调用函数之前等待特定 URL 加载

标签 python selenium url webdriver webdriverwait

这是我第一次在 stackoverflow 上发帖,我对 Selenium 和 Python 有点陌生。

当 URL 等于 fx: https://www.example.com 时,我不想运行函数。

我在另一个讨论中阅读了 this 答案,但我不太明白发生了什么。

我希望你能抽出时间来回答我的问题。

好的,所以我刚刚试过这个:

driver.get('https://www.google.com')
time.sleep(4)
driver.get('https://www.stackoverflow.com')

if WebDriverWait(driver, 10).until(EC.url_to_be('https://stackoverflow.com')):
    print('Desired url was rendered within allocated time')
else:
    print('Desired url was not rendered within allocated time')

但它没有用。有任何想法吗?
控制台说
Traceback (most recent call last):
  File "/Users/holger/PycharmProjects/waitTest/wait.py", line 15, in <module>
    if WebDriverWait(browser, 10).until(EC.url_to_be('https://www.stackoverflow.com')):
  File "/Users/holger/PycharmProjects/waitTest/venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

最佳答案

如果您的用例是在 url 等于 https://www.example.com 后运行一个函数,则您会导致 WebDriverWait 与以下任一 expected_conditions 不相联:

  • url_changes(url) :检查当前 url 的期望值,该 url 不能完全匹配。
    WebDriverWait(driver, 30).until(EC.url_changes("https://www.example.com"))
    
  • url_contains(url) :期望当前页面的 URL 包含特定文本。
    WebDriverWait(driver, 30).until(EC.url_contains("example"))
    
  • url_matches(pattern) :期望 URL 匹配特定的正则表达式。
    WebDriverWait(driver, 30).until(EC.url_matches("a_matching_pattern_of_the_expected_url"))
    
  • url_to_be(url) :期望当前页面的 URL 是一个特定的 url。
    WebDriverWait(driver, 30).until(EC.url_to_be("https://www.example.com"))
    
  • 注意 :您必须添加以下导入:
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    

  • 但是,WebDriverWait 结合上面提到的 expected_conditions 可能并不能保证 DOM Tree 中的所有元素都被完全加载。

    You can find a detailed discussion in Do we have any generic function to check if page has completely loaded in Selenium



    更新

    要在 WebDriverWait 返回 True 时运行函数,您可以使用以下解决方案:
    try:
        WebDriverWait(driver, 30).until(EC.url_to_be("https://www.example.com")))
        print("Desired url was rendered with in allocated time")
        # now you can call the method/function
        # test_me("Holger")
    except TimeoutException:
        print("Desired url was not rendered with in allocated time")
    

    注意 :您必须添加以下导入:
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    

    引用

    您可以在以下位置找到相关的详细讨论:
  • How to Wait for a Redirect Chain to Settle using Selenium WebDriver where final page loaded is not predictable?
  • 关于python - 如何在使用 Selenium 和 Python 调用函数之前等待特定 URL 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59732727/

    相关文章:

    php - 表单提交取消了我的 URL 查询字符串中的参数

    c++ - delphi 代码编译成 obj 文件以便在 python 中使用

    python - 我的python 3代码与python 2有何不同?

    python - 使用字符串输入创建类实例(python)

    javascript - 如何在地址栏上显示不同网址的新窗口中打开链接

    javascript - 向下滚动单个页面时是否可以更改网址

    python - MySQLdb - 检查行是否存在 Python

    c# - 在表中找不到列名关键字

    python Selenium : Explicitly wait for one of two elements to be loaded

    c - 将 Selenium 与 C 一起使用