Author - StudySection Post Views- 93 views

Selenium Webdriver Using python tutorial

Selenium Webdriver is an automation framework that allows us to interact with a browser. It supports cross-browser & cross-OS testing. Python is an easy to use language that allows the fast development of our script. It provides a large set of libraries to help us.

We are going to use ‘Chrome driver’ for this. You can just change the name of the web driver to interact with the browser of your choice. You can find out how to set up selenium with chrome using this article. Setup selenium with chrome

Suppose we want to login to Gmail. Here’s how you would do it:

  1. First import the selenium and time libraries:
    • In first library, we are importing webdriver module which will allow us to conduct different operations.
    • From time library, we can use time.sleep(s) to stop execution for ‘s’ seconds. It helps in avoiding errors that arise when the page is not loaded properly.

  2. Type these lines:

    • First-line tells which browser is used to execute the script. You could have used Firefox as well. The variable name could have been anything else. The most common are driver and browser.
    • Next, we give the URL of the site that we want to reach.
    • In the next line, we are maximizing the browser window. You may skip it if it is comfortable using the full window.
    • Using implicit wait, we are waiting for the page to load properly before we do any of the operations

  3. Next, we’ll find some locators to identify elements on the web page and do operations on them.

Locators are a way of finding one or more elements on a web page.

  1. Go to the URL on the browser, right-click on the element you want to pick and click on inspect element(DOM: Document Object Model).
  2. Check for attributes such as ID, Name, Xpath, etc.
  3. Copy it and use it as follows.

  • find_element_by_id – Try to use the Id wherever it is available as it is always unique. However, if the ID and name are the same, you can use whatever you want.
  • find_element_by_name – Syntax of name is similar to Id.

  • find_element_by_xpath – Where No Id or name is given, xpath can be used. Right click on the element, hover over copy and click on copy Xpath.
  • Find_element_by_link_text – You can directly use these lines without using a variable.
  • Find_element_by_partial_link_text – You can just use some part of the text to identify the element. If there are multiple elements with text ‘Learn’, it will pick the first element that has “Learn” in it. This is true for all the locators.You will have to use some other locator such as xpath or link_text etc or whatever is available.
  • Find_element_by_tag_name – sometimes there is no attribute that can be useful in identifying the element. In that case you can use tag name. Generally it is used when you want to find multiple elements having that tag. Eg – use anchor tag
  • Find_element_by_class_name – class is very useful when you want to find multiple elements of a particular class, however, it is useful for finding single element as well.
  • Find_element_by_css_selector – Like Xpath, it is useful when trying to identify a button, input etc. Right click on the element, hover over the ‘copy’ and click on ‘Copy Selector’. This might seem complicated but you just have to copy it. I could have taken a simpler example but I wanted to show you the full picture.

Apart from the above methods, there’s another way to find an element:

Note – It is required to import “from import By”. This method is useful when you need to use an explicit wait. I’ll explain this in the next Post.

Here’s the complete Code for logging into your Gmail account.

Note – This is a working example, you can run it as it is. You can do this without defining it inside a function but I suggest using a function So that you can call it anywhere anytime.

StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection’s .NET certification exam and get a .NET certification for enhancement of carrier in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection

Leave a Reply

Your email address will not be published. Required fields are marked *