Basic Selenium Methods and Usage
In this lesson, we will explore some key methods that are frequently used in Selenium
.
Opening a Web Page with get
In Selenium, the get
method is the most basic method to navigate to a specific URL.
It performs the same action as entering a URL in the browser's address bar and pressing enter.
from selenium import webdriver
# Create Chrome WebDriver
driver = webdriver.Chrome()
# Navigate to a specific URL
driver.get("https://www.example.com")
# Print the page title
print(driver.title)
Finding a Specific Element with find_element
To find a specific HTML element on a web page, we use the find_element
method.
For example, you can use an HTML element's ID, class name, HTML tag name, XPath, etc., to find a specific HTML element.
Note: XPath is a language used to select elements or attributes using a specific path expression. For instance,
//h1
selects all h1 elements in the page, and//button[@id='exampleId']
finds the button element with the IDexampleId
.
# Finding an element by ID
# find_element("id", "element ID")
element = driver.find_element("id", "exampleId")
# Finding an element by class name
# find_element("class name", "class name")
element = driver.find_element("class name", "exampleClass")
# Finding an element by HTML tag name
# find_element("tag name", "HTML tag name")
element = driver.find_element("tag name", "button")
# Finding an element by XPath
# find_element("xpath", "XPath expression")
element = driver.find_element("xpath", "//button[@id='exampleId']")
Using the By
class provided by Selenium allows you to find elements more concisely.
from selenium.webdriver.common.by import By
# Finding an element by ID
element = driver.find_element(By.ID, "exampleId")
# Finding an element by class name
element = driver.find_element(By.CLASS_NAME, "exampleClass")
# Finding an element by tag name
element = driver.find_element(By.TAG_NAME, "button")
# Finding an element by XPath
element = driver.find_element(By.XPATH, "//button[@id='exampleId']")
To extract text from the selected element, use the text
attribute.
# Extract text from the element
element_text = element.text
# Print the extracted text
print(element_text)
Clicking an HTML Element with click
To click a specific HTML element on a web page, use the click
method.
# Click the found element
element.click()
For instance, you can use Selenium to automatically click a login button or follow a specific link.
Entering Text with send_keys
To automatically enter text into an input field, use the send_keys
method.
This method inputs text as if the user is typing on the keyboard.
# Enter text into the input field
element.send_keys("Hello, World!")
This is very useful for automating tasks like entering a username and password into a login form.
Closing the Web Page
Closing the Browser with quit
If you want to close the browser after completing all tasks, use the quit
method.
The quit method closes all open browser windows and ends the WebDriver session.
# Close the browser
driver.quit()
This helps prevent memory leaks (the phenomenon where unnecessary data remains in memory) and allows you to close the browser cleanly.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.