Friday 7 May 2021

Different type of locators in webdriver selenium

 Webdriver locators

ID

This can be used when you know the id attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"



from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")


Name

This can be used when you know the name attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_name("pw").send_keys("Password")



Xpath

This can be used when you know the xpath attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

//tagname[@attribute='value']

from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_name("pw").send_keys("Password")
driver.find_element_by_xpath("//input[@id='Login']").click()

We can also write regex for xpath as below

*[contains(@attribute,'value')]

Let us see one example for the same


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_xpath("//*[contains(@class,'password')]").send_keys("Roshni")
driver.find_element_by_xpath("//input[@id='Login']").click()

CSS

This can be used when you know the CSS attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

//tagname[@attribute='value']

input[id='password']

from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_css_selector("input[id='password']").send_keys("Roshni"))
driver.find_element_by_xpath("//input[@id='Login']").click()

We can also write regex for css as below

[attribute*,'value']

Let us see one example for the same


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)
driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_css_selector("[class*='password']").send_keys("Roshni")
driver.find_element_by_xpath("//input[@id='Login']").click()

Generating CSS from ID

tagname#ID - tagname is again optional here

eg. input#username

Only used when you know id attribute or classname. For classname you have to put "."


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_css_selector("input#username").send_keys("Roshni")
driver.find_element_by_xpath("//*[contains(@class,'password')]").send_keys("Roshni")
driver.find_element_by_xpath("//input[@id='Login']").click()

CSS method of finding elements on a webpage is most fastest way to find elements. So I would recommend to use css method than to use any other while writing your code for selenium.

Classname

This can be used when you know the classname attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_css_selector("input#username").send_keys("Roshni")
driver.find_element_by_class_name("password").send_keys("roshni")
driver.find_element_by_xpath("//input[@id='Login']").click()

Generating xpath based on text

//tagname[text()='aaa']

driver.find_element_by_xpath("//label[text()='Password']")

linktext

This can be used when you know the linktext attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

driver.find_element_by_link_text("Forgot Your Password?").click()





Thursday 6 May 2021

Why Selenium using Python?

 Why Selenium using Python?

There are n number of reasons to learn selenium using python

  • Python is open source and lots of packages are available on internet which can be utilized in your framework.
  • Python is easy to learn if you know any basic programming language.
  • writing code in python is also simpler than any other languages
  • Industry is moving towards python and hence learning automation framework using python would add value.

 Let us start with selenium with python and slowly we will build a pytest framework using this

Which editor to use for coding?

I have used pycharm as it is easy to write code here and to debug. You can download from below link

https://www.jetbrains.com/pycharm/download/

How to install selenium?

Use below command for installing selenium on your machine

pip install selenium

Once selenium is installed on your machine you would see

C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Scripts>pip install -U selenium
Collecting selenium
  Using cached selenium-3.141.0-py2.py3-none-any.whl (904 kB)
Requirement already satisfied, skipping upgrade: urllib3 in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from selenium) (1.25.8)
Installing collected packages: selenium
Successfully installed selenium-3.141.0

To check whether selenium package is installed or not please check below url on system 

C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages

You should be able to see the selenium packages if properly installed.

How to invoke chrome browser in your selenium?

from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

chromedriver could be found from below url

https://chromedriver.chromium.org/downloads

Unzip the folder and later paste the chromedriver.exe at any location which you can invoke in your program.

Some basic webdriver methods


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://mobile-automation-testing.blogspot.com/p/adb-command-to-send-sms.html")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)
print(driver.name)
driver.back()
driver.refresh()
driver.minimize_window()
driver.close()

driver.get will get the url which need to be automated

driver.maximize_window would maximize the chrome window

driver.title would get the webpage title

driver.name would get the name of browser

driver.back() would get back to the previous page

driver.refresh() would refresh the current webpage

As there are many methods under webdriver you would know each by learning further in this blog

Inspecting HTML to identify attribute of elements


Different type of locators in webdriver selenium

 Webdriver locators ID This can be used when you know the id attribute of an element. If the element is not found by mentioned attribute, it...