Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

regex - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...