Selenium in python is used for browser automation. We need to install chromium webdriver also for automating chrome. In python jupyter notebook first we need to install selenium as given below

pip install selenium
from selenium import webdriver
driver = webdriver.Chrome(location of chromium webdriver)
driver.get("website you want to open")

there are 8 types of locators in selenium.

Locators Description
class name locate elements by class name
xpath locate elements by xpath
id locate elements by id
name locate elements by name
link text locate elements by link text
partial link text locate elements by partial link text
tag_name locate elements by tag name
css selector locate elements by css selector
Locators in Selenium Python

XPATH

we can copy xpath as given below

  • First Inspect webpage
  • Right click and click on copy
  • then copy XPATH and xpath will be copied.
Xpath locators in selenium in python
How to copy XPATH
from selenium.webdriver.common.by import By
create = driver.find_element(By.XPATH, "xpath of element")
create.click()

CLASS NAME

Similarly element can be located by class name & value can be found by inspecting web pages as shown below

Class locators in selenium in python

below code will find element by class_name and click on it.

from selenium.webdriver.common.by import By
create = driver.find_element(By.CLASS_NAME,"class")
create.click()

CSS_SELECTOR

Syntax of CSS Selector is

HTML TAG[Locator=Value]

  • HTML tag like span,div etc
  • Locator like id,title,name etc
  • Value is what given in locator id, title etc

Below is code for finding element via css selector. It will find elements and will click on it.

ep=driver.find_element(By.CSS_SELECTOR,"span[title='Export page']")
ep.click()

We can find elements by text associated with a link. Downloads, about community are the link text as shown in below image.

ID

Elements can be located by id by inspecting webpage.

x=driver.find_element(By.ID,"resolved")
x.click()

NAME

We can locate elements by name attribute present on webpage

x=driver.find_element(By.NAME,"resolved")
x.click()

 

By SC

2 thoughts on “Selenium in Python”

Comments are closed.