简体   繁体   中英

expected_conditions with Selenium [Python]

I want to load my driver until my current_url contains "something". I have tried the following code:

self.url = self.driver.current_url
try:
    element = WebDriverWait(self.driver, 20).until(EC.title_contains("XXX", "YYY", "ZZZ"))
except:
    print "\n IMPERFECT URL \n"
finally:
    self.driver.quit()

But this approach uses title search .. I want to check my current url for possible sets of strings. How do I do that? Also I want to check for three sets of strings in the same url. Could somebody help. I am a newbie in Selenium.

I'm not quite getting what exact test you want to perform. At any rate, you can pass a callable and test for anything you want. Here is an example of working code that tests whether google , blah or foo are present in the current URL:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome("/home/ldd/src/selenium/chromedriver")

driver.get("http://google.com")

def condition(driver):
    look_for = ("google", "blah", "foo")
    url = driver.current_url
    for s in look_for:
        if url.find(s) != -1:
            return True

    return False

WebDriverWait(driver, 10).until(condition)

driver.quit()

(Obviously, the path to the Chrome driver has to be adapted to your own situation.)

As soon as the return value of condition is a true value, the wait ends. Otherwise, a TimeoutException will be raised. If you remove "google" from ("google", ...) you'll get a TimeoutException .

I think in the expected_conditions class not exists a definition for what you want. But you can define your own expected_conditions , in this two questions are provided a good explanations about this topic :

In any case you could use lambda expressions to define your function in the WebDriverWait .

I hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM