Selenium is an automated testing tool mainly used to simulate user interactions in web applications. Although it was originally designed for automated testing, it is also widely used for web scraping, web automation, and web testing.
1. Install and set up Selenium
First, you need to install the Selenium library. Use pip to install Selenium:
pip install selenium
Additionally, a browser driver needs to be downloaded and installed so that Selenium can communicate with the browser. Selenium supports multiple browsers, including Chrome, Firefox, Edge, etc. Choose the appropriate browser driver according to your needs.
Taking the Chrome browser as an example, you need to download the Chrome driver and add it to the system’s PATH environment variable.
2. Use Selenium to open web pages
First, let’s take a look at how to use Selenium to open a web page:
from selenium import webdriver #Create a Chrome browser instance driver = webdriver.Chrome() # open the Web page driver.get("https://www.example.com") # Close browser driver.quit()
This code creates a Chrome browser instance and then opens the specified web page. Finally, close the browser via the quit()
method.
3. Positioning and interacting with HTML elements
Selenium locates HTML elements in different ways, such as ID, class name, tag name, XPath, etc.
Here are some examples:
# Locate element by ID element = driver.find_element_by_id("element_id") # Locate elements by class name element = driver.find_element_by_class_name("element_class") # Locate elements by tag name element = driver.find_element_by_tag_name("element_tag") # Locate elements via XPath element = driver.find_element_by_xpath("//div[@class='example']")
Once the element is positioned, interact with it, such as clicking, entering text, getting text content, etc.
# Click element element.click() # Enter text element.send_keys("Hello, Selenium!") # Get the text content of the element text = element.text
4. Process the form
Selenium can also be used to process form elements such as input boxes, radio boxes, check boxes, and drop-down boxes.
Here are some examples:
# Enter text into the text box text_input = driver.find_element_by_name("username") text_input.send_keys("my_username") # Select radio button radio_button = driver.find_element_by_id("radio_button_id") radio_button.click() # Select checkbox checkbox = driver.find_element_by_name("agree_checkbox") checkbox.click() #Select drop-down box options from selenium.webdriver.support.ui import Select select = Select(driver.find_element_by_id("dropdown_id")) select.select_by_visible_text("Option 2")
5. Execute JavaScript code
Sometimes, JavaScript code may need to be executed to interact with the page or modify the page content. Selenium allows executing JavaScript code:
# Execute JavaScript code driver.execute_script("alert('Hello, Selenium!');")
This will display an alert box on the page.
6. Working with windows and tabs
Selenium can handle multiple windows and tabs. Use the following methods to switch windows:
# Get the current window handle current_window = driver.current_window_handle # Get all window handles all_windows = driver.window_handles #Switch to another window driver.switch_to.window(another_window)
7. Waiting and timeout
Waiting is an important concept used to ensure that a page is loaded or an element is visible. Selenium provides different types of waits, such as implicit waits and explicit waits:
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Implicit wait driver.implicitly_wait(10) # Wait up to 10 seconds #Explicit wait wait = WebDriverWait(driver, 10) element = wait.until(EC.presence_of_element_located((By.ID, "element_id")))
This ensures that the code waits for the element to appear within the wait time, or continues execution after a timeout.
8. Browser operation
Selenium also supports some browser operations, such as forward, backward, refresh, etc.:
# Forward driver.forward() # Back driver.back() # refresh driver.refresh()
9. Handling pop-up boxes
If there are pop-ups on the page, use the following methods to handle them:
# Get the pop-up box alert = driver.switch_to.alert # Get the pop-up box text alert_text = alert.text # Accept the pop-up box (click the OK button) alert.accept() # Cancel the pop-up box (click the cancel button) alert.dismiss()
10. Practical application examples
The following is a practical application example, using Selenium to automatically log in to a website:
from selenium import webdriver #Create a Chrome browser instance driver = webdriver.Chrome() # Open the login page driver.get("https://www.example.com/login") # Locate the username and password input boxes username_input = driver.find_element_by_name("username") password_input = driver.find_element_by_name("password") # Enter username and password username_input.send_keys("my_username") password_input.send_keys("my_password") # submit Form login_button = driver.find_element_by_id("login_button") login_button.click() # Wait for login to complete from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) wait.until(EC.presence_of_element_located((By.ID, "user_profile"))) #Operations after successful login #... # Close browser driver.quit()
This example demonstrates how to use Selenium to simulate a user logging into a website, entering a username and password, submitting a form, and waiting for the login to complete to perform other actions.
Summary
Python Selenium is a powerful tool for automated web testing, data scraping, and task automation. This article introduces all aspects of Python Selenium in detail, including basic concepts, installation and configuration, common methods and techniques, etc.
The power of Python Selenium is its cross-browser support, allowing testing and data scraping in different browsers. Selenium Grid can also be used to execute tests in parallel on multiple remote machines. Most importantly, Python Selenium’s ecosystem is vast, with rich extensions and libraries to meet various needs.
Whether you are a developer, test engineer or data analyst, Python Selenium is a tool worth mastering. Through the detailed introduction and sample code of this article, you can quickly master the basic usage of Python Selenium and apply it in actual projects to improve work efficiency and accuracy.
Take action, it’s better to be on the road than to wait and see. In the future, you will definitely thank yourself for working hard now! If you want to learn and improve but can’t find the information and there is no one to answer your questions, please join the group in time: 786229024. There are various test and development materials and technologies in which you can communicate together.
Finally: The complete software testing video tutorial below has been compiled and uploaded. Friends who need it can get it by themselves[Guaranteed 100% Free]
Software testing interview document
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeWeb crawlerSelenium386601 people are learning the system