Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 sure that there is a better way (as it's flooding that website with getting requests).

while flag:
    time.sleep(1)
    print(driver.current_url)
    if driver.current_url == "mydesiredURL_by_Regex":
        time.sleep(1)
        myfunction()

I was thinking to do that somehow with WebDriverWait but not really sure how.

like image 337
gyula Avatar asked Mar 30 '16 18:03

gyula


3 Answers

This is how I implemented it eventually. Works well for me:

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
desired_url = "https://yourpageaddress"

def wait_for_correct_current_url(desired_url):
    wait.until(
        lambda driver: driver.current_url == desired_url)
like image 74
Svetlana Levinsohn Avatar answered Oct 05 '22 22:10

Svetlana Levinsohn


To really know that the URL has changed, you need to know the old one. Using WebDriverWait the implementation in Java would be something like:

wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.not(ExpectedConditions.urlToBe(oldUrl)));

I know the question is for Python, but it's probably easy to translate.

like image 31
Hatya Avatar answered Oct 06 '22 00:10

Hatya


I was thinking to do that somehow with WebDriverWait

Exactly. First of all, see if the built-in Expected Conditions may solve that:

  • title_is
  • title_contains

Sample usage:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.title_is("title"))
wait.until(EC.title_contains("part of title"))

If not, you can always create a custom Expected Condition to wait for url to match a desired regular expression.

like image 35
alecxe Avatar answered Oct 06 '22 00:10

alecxe