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 |
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.
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
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()
LINK TEXT
We can find elements by text associated with a link. Downloads, about community are the link text as shown in below image.
x=driver.find_element(By.LINK_TEXT,"Downloads")
x.click()
Above code will click on download section of website.
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()
[…] we need to upload Keys from selenium webdriver & […]
[…] data from website is termed as web scrapping.we can use selenium and python for web […]