Skip to main content

Posts

Showing posts from October, 2018

Iframes, Implicit and Explicit waits in Selenium using Python with example

Modern web-technologies uses AJAX for loading data instead of refreshing the whole page. With this approach, if an element is not present in DOM, an exception will be raised. To understand this better, I have created a small example on JSFiddle , which uses JQuery to fetch some data from a REST API and then update the page with the results using AJAX. If we do not use any delay, our test case will fail. One solution could be to use time.sleep() after clicking the button to wait for the response. from selenium import webdriver import unittest import time CHROMEDRIVER = "/Users/sarbjit/webdrivers/chromedriver" WEBSITE = "https://jsfiddle.net/sarbjit87/cf3ud4qp/" class TestJSFiddle (unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(executable_path=CHROMEDRIVER) def test_page(self): self.driver.get(WEBSITE) time.sleep(5) # Wait for website to load iframe = self.driver.find_element_by_xpath( "//

Starting with Selenium Web-Driver using Python by example

Selenium Web-Driver is an open source tool which is used to automate web-applications. It uses drivers to support different web-browsers. Selenium supports different language bindings for doing automation. In this post, I'll be using Python binding as an example. Our automation task for this post is as follows :- Open this website   Select the Login Page using login option from drop-down menu Enter user-name and password (fake credentials) Look for error message and compare it with the expected message Take a screenshot of the website for reference Before, we get into the example code, let's do the setup required for running Selenium. Install Selenium using pip ( pip install selenium ) Install the drivers for the web-browser of your choice. I am using Chrome Webdriver for my example. Visit this website and follow the links to install web-browser driver. In order to execute the following code, you need to run the " chromedriver " on a seperate te

Xpath query and examples

In this post, we'll look at the commonly used XPath queries. We'll be using the following XML file as an example <bookstore> <book category= "cooking" > <title lang= "en" >Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category= "children" > <title lang= "en" >Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category= "web" > <title lang= "en" >XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <ye

CSS Selectors

Type Selector :   Select elements by type e.g.  Select all h1 elements and set the color to red h1 {   color : red; } ID Selector :   Select elements by id e.g.  Select all elements with id equal to error #error {   color : red; } Descendant Selector :   Select element inside another element e.g.  Select all <strong> elements that are inside <p> p strong {   color : red; } e.g.  Select all <p> elements that are inside element with id container #container p {   color : red; } Class Selector :   Select element by their class e.g.  Select all elements with class set to warning .warning {   color : yellow; } Combining Class Selector :   Select element by their class e.g.  Select <p> elements with class set to warning p.warning {   color : yellow; } e.g.  Select <p> elements with class set to warning which are inside <div> div p.warning {   color : yellow; } Combining Type Selector by Comma :   Combine select