Selenium is an open source, free, simple, and flexible automated testing tool that supports web browsers well. It is very practical in scenarios such as UI automation and crawlers. Being able to master and use Selenium tools can greatly improve efficiency.
Introduction to Selenium
Selenium supports multiple platforms, multiple browsers, and multiple languages to realize automated testing. It is an open source and portable web testing framework that supports parallel test execution, thereby reducing time and improving testing efficiency. Using it, we can write related automation programs, so that the program can operate the web interface in the browser exactly like a human, such as simulating mouse clicks, simulating keyboard input, and so on. Not only can you operate the Web interface, but you can also obtain information from the Web. Relatively speaking, it is easier to use Selenium to obtain information. Its basic principle is that after we write an automation program, we use the browser driver to directly operate the browser. As long as we The information that users can obtain on the browser can be obtained using Selenium.
Environmental preparation
Download the browser driver, note that the driver version must be consistent with the browser version
Add the browser driver path to the environment variable path
Install the Selenium package pip install selenium
quick start
Use selenium to control the browser to open the Baidu homepage and search for Alipay.
from selenium import webdriver # import webdriver
import time
driver = webdriver.Chrome() # get browser driver
driver.get("http://www.baidu.com") # Open Baidu homepage
input_box = driver.find_element_by_id('kw') # Get the homepage input box element
input_box.send_keys('Alipay') # Input content into the input box
search_button = driver.find_element_by_id('su') # Get the home page search button element
search_button.click() # Click the search button
time.sleep(5)
driver.quit() # close the driver
Selenium-API operation [free sharing of Selenium automated testing learning resources at the end of the article]
element wait
show wait
Set a timeout period, check whether the element exists every once in a while, and execute the subsequent content if it exists, and throw a timeout exception (TimeoutException) if it exceeds the maximum time (timeout period). Display waiting needs to use WebDriverWait, and at the same time cooperate with until or not until.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('Http://www.baidu.com')
# Detect whether the element exists every 0.5 seconds, and throw an exception if it is not found within 5 seconds
element = WebDriverWait(driver, 5, 0.5).until(
expected_conditions.presence_of_element_located((By.ID, 'kd')), message='element not found')
implicit wait
Implicit waiting also specifies a timeout. If the specified element has not been loaded beyond this time, a NoSuchElementException will be thrown. In addition to the different exceptions thrown, there is another point that the implicit wait is global, that is, during the running process, if the element can be located, it will not affect the code running, but if it cannot be located, it will use polling The method continues to access the element until the element is found, and if the specified time is exceeded, an exception is thrown. Using implicitly_wait() to implement implicit waiting is much simpler than explicit waiting.
driver.implicitly_wait(5)
forced wait
Use time.sleep() to force waiting, set a fixed sleep time, and wait for elements to load, which will affect the running efficiency of the code.
element positioning
Selenium provides 8 basic element positioning methods, namely id, name, class name, tag name, link text, partial link text, xpath, css selector, where id, name, class name, and tag name are based on the label of the element Or the attribute of the element to locate; link text, partial link text is located according to the text of the hyperlink; xpath is the element path location; css is the selector location (style location).
element = driver.find_element_by_id('kw') # get element by id attribute
element = driver.find_element_by_name('wd') # get element by name attribute
element = driver.find_element_by_class_name('input') # get element by class attribute
element = driver.find_element_by_tag_name('input') # get element by tag name
element = driver.find_element_by_link_text('video') # get element by link text value
element = driver.find_element_by_partial_link_text('view')# get element by partial link text value
element = driver.find_element_by_xpath("//*[@id='kw']") # get element by Xpath
element = driver.find_element_by_css_selector('#kw') # get element by CSS selector
element manipulation
After locating the element and obtaining the element object, we can perform the operations we want on the element. Selenium provides many APIs for us to operate elements. Common operations include click, input, clear, get element coordinate value, get element width and height value, element attribute value, check if element is selected.
elemnet.click() # click on the element
element.send_keys('Wuhan') # input content
element.clear() # clear content
element.location.get('x') # Get the X-axis coordinates of the upper left corner of the element
element.location.get('y') # Get the Y-axis coordinates of the upper left corner of the element
element.size.get('width') # get element width
element.size.get('height') # get element height
element.is_selected() # Whether the element is selected
mouse operation
Common mouse operations include: click, right click, double click, hover, drag and drop, etc. Selenium encapsulates corresponding operation methods for these mouse operations
In Selenium, the method of operating the mouse is encapsulated in the ActionChains class. All the mouse event methods provided in the ActionChains class are stored in the ActionChains object when they are called, and the perform() method is to actually execute all. mouse events.
from selenium.webdriver.common.action_chains import ActionChains # 导包
actionChains = ActionChains(driver) # instantiate ActionChains object
actionChains.move_to_element(element).perform() # mouseover
actionChains.drag_and_drop(elementA, elementB).perform() # mouse drag
actionChains.context_click(element).perform() # mouse right click
actionChains.double_click(element).perform() # Double click the mouse
keyboard operation
In Selenium, the keys of the keyboard are encapsulated in the Keys class to simulate the input of some keys or combination keys on the keyboard, and use send_Keys+Keys.XXX to realize the combination keys on the keyboard such as: Ctrl+C, Ctrl+V.
from selenium.webdriver.common.keys import Keys # 导包
element.send_keys(Keys.BACK_SPACE) # Simulate pressing the backspace key
element.send_keys(Keys.CONTROL, 'a') # Simulate pressing Ctrl+A
element.send_keys(Keys.CONTROL, 'x') # Simulate pressing Ctrl+X
element.send_keys(Keys.CONTROL, 'v') # Simulate pressing Ctrl+V
browser operation
Selenium also provides corresponding APIs for browser operations, such as maximizing the browser window, setting the size of the browser window, setting the position of the browser, controlling the browser to move forward and backward, and refreshing the page.
driver.maximize_window() # Maximize the browser window
driver.set_window_size(800, 800) # The browser window is 800 wide and 800 high
driver.set_window_rect(300, 0) # browser position (300,0)
driver.back() # browser back
driver.forward() # browser forward
driver.refresh() # Refresh the page
other operations
Bookmark page
driver.get_screenshot_as_file('./baidu.png') # page screenshot
driver.close() # close the current tab
driver.quit() # close all tabs
Clicking a link on a certain page opens a new tab page, and selenium cannot locate the element of the new tab page at this time. This involves the concept of the handle, which is the unique identifier of the tab page object. Each tab page has its own handle, and the tab page can be switched through the handle to locate the elements of the corresponding tab page.
handles = driver.window_handles # Get the handles of all tabs
driver.switch_to.window(handles[1]) # switch to the specified handle tab
startup parameters
Chrome Options is a class that configures the properties of chrome when it starts. Through this parameter, we can add startup parameters for Chrome.
Set the chrome binary file location (binary_location)
Add startup parameters (add_argument)
Add extension application (add_extension, add_encoded_extension)
Add experimental settings parameter (add_experimental_option)
Set the debugger address (debugger_address)
The common behaviors of Chrome Options generally include the following:
Disable the loading of pictures and videos: Improve the loading speed of web pages.
Add proxy: Anti-climbing technology used to access certain pages over the wall, or to deal with IP access frequency restrictions.
Use the mobile head: visit the site on the mobile end. Generally, the anti-crawling technology of this site is relatively weak.
Adding extensions: Functions like using the browser normally.
Set encoding: deal with Chinese websites and prevent garbled characters.
Prevent JavaScript from executing
Before opening the browser in UI automation, you can add the option configuration of the browser. By setting different parameters, you can modify the default behavior of the browser.
from selenium import webdriver
options = webdriver.ChromeOptions() # instantiate a startup parameter object
options.add_argument('--headless') # Set the browser browser does not provide visual pages
options.add_argument('lang=zh_CN.UTF-8') # set encoding
options.add_argument('--disable-infobars') # Disable strategy
options.add_argument('--no-sandbox') # Solve the error that the DevToolsActivePort file does not exist
options.add_argument('window-size=1920x1080') # Specify browser resolution
options.add_argument('--disable-gpu') # Google documentation mentions that this attribute needs to be added to avoid bugs
options.add_argument('--incognito') # Incognito mode (incognito mode)
options.add_argument('--disable-javascript') # 禁用javascript
options.add_argument('--start-maximized') # Maximize operation (full screen window), if not set, an error will be reported when fetching elements
options.add_argument('--disable-infobars') # Disable the prompt that the browser is being controlled by an automated program
options.add_argument('--hide-scrollbars') # hide scrollbars, to deal with some special pages
options.add_argument('blink-settings=imagesEnabled=false') # Do not load images, improve speed
options.add_argument('User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36
') # Set UA request header
driver = webdriver.Chrome(chrome_options=options)
Execute JS script
In some cases, the API provided by selenium cannot complete the corresponding operation or the operation is cumbersome. In this case, JS scripts can be used to implement it, such as executing the sliding scroll bar.
js ='window.scrollTo(0,100)' # JS script statement to be executed
driver.execute_script(js) # execute JS script
Frame switching
Usually, most website pages use frame nesting. At this time, even if the content of the frame nested page is displayed, we still cannot directly locate the elements in the frame, such as the login window like Youku.
At this time, if you want to operate the elements in the frame, you need to switch to the frame before positioning.
driver.switch_to.frame('alibaba-login-box') # and switch to the specified frame
driver.switch_to.default_content() # Switch back to the default page
Hiding Fingerprint Features
Most of the browsers started by slenium will be detected by the website through some fingerprint features. If the crawler behavior is recognized, the operation of selenium will be rejected. How to avoid this situation, the key point is how to use these features before browser detection Hidden, in fact, the predecessors have paved the way for us. The key to solving this problem is actually a stealth.min.js file. This file is for puppeteer. If you use it in Python, you need to execute this file separately. The way to obtain this file requires installing node.js, execute npx extract-stealth-evasions on the terminal to download the file, and add the following code before operating the browser.
with open('stealth.min.js') as f:
js = f.read()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {'source': js})
epilogue
This article just briefly introduces the installation and use of selenium+python, as well as some basic and commonly used API operations, and there are many advanced operations and usages that require more in-depth understanding and learning. For practical use, it may also need to be integrated with other frameworks and tools, which require deeper understanding and learning.
Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free 【保证100%免费】
Software testing interview applet
The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!
The following interview question sections are covered:
1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux
6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics
How to obtain the full set of information: