Showing posts with label selenium with python. Show all posts
Showing posts with label selenium with python. Show all posts

So in last article we have seen how to locate the WebElement using four locators and those are Name, Id, Partial Link Text and Link Text. Now in this article we are going to see the remaining four locators. Let’s start looking at them one by one.

Locating element By Xpath:
Xpath functions are very useful functions, it help to identify the Elements from its root node as well as from anywhere in the page. As HTML can be an execution of XML (XHTML), Selenium clients can use this amazing language to target WebElement in their web applications. XPath goes beyond (and additionally supporting) the straightforward techniques for situating by id or name attributes, and opens up a wide range of new conceivable outcomes, for example, finding the third checkbox on the page.

In this article, we are going to see how we can locate the web element to perform an action on it. Web element is the elements which are present on a webpage. Button, link, images, text boxes etc. this all are called as web element.

So why do we need to locate the WebElement? The simple answer for this question is, we need to locate the address of each WebElement on which we want to perform actions. Actions like entering the text, copying the text, pressing the buttons etc. actions 

By reading the above paragraph, we hope that you got an idea why we need to locate the WebElement. Now let’s see how we can locate the WebElement.

Today we are going to see the basic things which we are going to use while making the selenium WebDriver scripts using python language. So below is some short example which we are going to see the uses of each line statement

From selenium import webdriver

From selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox ()

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

Assert "Python" in driver.title

Element = driver.find_element_by_name ( " address of webelement " )

Element.clear ()

Element.send_keys ( "content text" )

Element.send_keys ( Keys.RETURN )

Assert "No results found." not in driver.page_source

driver.close ()

The above script can be saved to a file (eg: - python_org_search.py ), then you can use it like this

Today we are going to see the installation of Python in Eclipse, so that we can use the python as a scripting language for writing the script for selenium WebDriver. Before that let’s see why python is preferred over java.

Preference Python over Java with Selenium  

There is an observation that most of the times python is used with selenium due to following points:
  1. Python is favored programming language as it runs faster than Java programs.
  2. Python uses indentation which is easy to use rather than using traditional braces to start and end block of code while programming.
  3. Python is easy to code due to dynamic typing as compared to static typing in java which can increase chances of syntactical error.
  4. Python is compact and easy to handle as compared to Java.