What is the best way to check URL change with Selenium in Python?

前端 未结 5 2030
我在风中等你
我在风中等你 2020-12-10 15:42

So, what\'s I want to do is to run a function on a specific webpage (which is a match with my regex).

Right now I\'m checking it every second and it works, but I\'m

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 16:00

    Here is an example using WebdriverWait with expected_conditions:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC 
    
    url = 'https://example.com/before'
    changed_url = 'https://example.com/after'
    
    driver = webdriver.Chrome()
    driver.get(url)
    
    # wait up to 10 secs for the url to change or else `TimeOutException` is raised.
    WebDriverWait(driver, 10).until(EC.url_changes(changed_url))
    

提交回复
热议问题