2

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.

Saheb
  • 1,666
  • 3
  • 18
  • 24

2 Answers2

2

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.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • I am performing a submit operation in a form. After submit the current page redirects to another page. I want to fetch the url of the redirected page. Did you get it? – Saheb Jan 31 '14 at 08:15
  • I've updated my answer. I've imagined some sort of test you could do on ``driver.current_url`` but I'm not sure that's what you had in mind. At any rate, the principle is fully illustrated in my answer. – Louis Jan 31 '14 at 12:31
0

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.

Community
  • 1
  • 1
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • I'm quite a newbie and didn't understand the links you referred. And yes there's no EC class for what I want. Do you have any other links? – Saheb Jan 31 '14 at 08:16