System environment :Ubuntu20.04
selenium Is a Web Application testing Tools for .Selenium Test runs directly in browser , It's like a real user is doing it . Supported browsers include IE(7~11)、Mozilla Firefox、Safari、Google Chrome、Opera、Edge etc. . The main functions of this tool include : Test compatibility with browser —— Test your application to see if it works well on different browsers and operating systems . Test system functions —— Create regression tests to verify software functionality and user requirements . Support automatic recording of actions and automatic generation .Net、Java、Perl Test scripts in different languages .
selenium Installation instructions :
sudo pip3 install selenium
With google-chrome And chromedriver For example .
First , Inquire about google-chome edition .
google-chrome --version
then , Find the closest version at the address below ( Otherwise it won't work ) Of chromedriver Download decompression .
CNPM Binaries Mirrorhttp://npm.taobao.org/mirrors/chromedriver/ next , stay chromedriver Open under the decompression path terminal, Execute the following command to replace the system's chromedriver.
sudo mv -f chromedriver /usr/local/share/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
chromedriver --version
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
url = 'https://blog.csdn.net/qq_15711195?spm=1018.2226.3001.5343'
option = webdriver.ChromeOptions()
option.add_experimental_option('detach', True)
browser = webdriver.Chrome(options=option)
browser.get(url)
# modify name Parameter values
new_name = 'haha'
name = browser.find_element_by_id('name')
name.clear()
name.send_keys(new_name)
# Check flag-checkbox Check the box . If subsequent modifications are made on this basis , Have to wait for a while
flag_checkbox = browser.find_element_by_id('flag-checkbox')
if not flag_checkbox.is_selected():
flag_checkbox.click()
sleep(1)
# Click the save button . When button There is a coating on it div when , It is best to xpath Positioning buttons in the form of statements
button_path = "//div[@class='btn-submit']/button[@type='button']"
save_button = browser.find_element_by_xpath(button_path)
save_button.send_keys(Keys.ENTER)
# When performing a click operation , It can also be done in the following way . Web pages may get stuck when information is lost .
# browser.execute_script('arguments[0].click()', save_button)
# Can directly quit. When manually closing a web page in close Before , It can lead to close Report errors .
browser.close()
browser.quit()
【1】 What is? Selenium WebDriver_ Simple happiness _wsh The blog of
【2】Selenium WebDriver Basic operation tutorial _bizcatt The blog of ( contain driver download )
【3】 How to be in Ubuntu Lower installation configuration Selienium And chrome webdriver_nice970111 The blog of