100 Days of Code: Day 48
Notes of 100 Days of Code: The Complete Python Pro Bootcamp.
Resources: https://www.selenium.dev/
Document: https://selenium-python.readthedocs.io/
Main difference between Selenium and BeautifulSoup:
BeautifulSoupis for reading static HTML.Seleniumis for interacting with a live browser and dynamic content.
Selenium
Methods to locate elements: find_element or find_elements (to find multiple elements, which will return a list).
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')
Attributes which are available for By class:
ID = "id"
NAME = "name"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
Example:
find_element(By.ID, "id")
find_element(By.NAME, "name")
find_element(By.XPATH, "xpath")
find_element(By.LINK_TEXT, "link text")
find_element(By.PARTIAL_LINK_TEXT, "partial link text")
find_element(By.TAG_NAME, "tag name")
find_element(By.CLASS_NAME, "class name")
find_element(By.CSS_SELECTOR, "css selector")
If want to locate a specific element in certain class, e.g. <a> wrapped in class event-widget
driver.find_element(By.CSS_SELECTOR, ".event-widget a")
What is XPath?
XPath is a major element in the XSLT standard.
XPath can be used to navigate through elements and attributes in an XML document.
How to find XPath?
With inspect on browser page and right click the element, then find Copy -> Copy XPath in the right-click menu.