Skip to content

Selenium Web Driver: some tricks using Python

March 5, 2012

After migrating to Web Driver it took time to get with the following things:

* Selecting element from drop down list (now not just select command), e.g.:

el = driver.find_element_by_id('id_line')
for option in el.find_elements_by_tag_name('option'):
    if option.text == "line to select":

* If there are lot of element existence checks that wrapped in try-catch block it is useful to reduce implicit timeout to reduce overall test execution time, e.g. for Firefox driver:

def setUp(self):
    self.driver = webdriver.Firefox()
    self.verificationErrors = []

* Try to avoid time.sleep(N) commands in tests it is better to wait for some action or change in system. So the best way to make construction:

    if self.is_element_present(By.CSS_SELECTOR, "h3 > strong"):
        self.assertTrue(int(driver.find_elements_by_css_selector("#video_source")[0].text) >= 0)
        raise Exception( + ".ErrorText!")
except Exception as e: self.verificationErrors.append(str(e))

or wait by small ticks:

for i in range(60):
        if self.is_element_present(By.LINK_TEXT, "Exit"): break
    except: pass


else:"time out")

* Xpath is always slower in IE
* If some elements in DOM are hidden you can access them using JavaScript execution command driver.execute_script(script, *args)


From → Selenium

  1. ryan permalink

    Why did you decide against using WebDriverWait in your third example? So something like:
    WebDriverWait(driver, 60).until(lambda driver : driver.find_element_by_link_text (“169_convertedvideo_425”)

  2. Actually it is the same but using another code structure 🙂

  3. sagarika permalink

    Help me to configure selenium web-driver under RUBY

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: