程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python web page information operation - webdriver

編輯:Python

System environment :Ubuntu20.04

One 、selenium and webdriver

1. What is? selenium?

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 

2. Browser and its corresponding driver install

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

Two 、 Code samples

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()

reference

【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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved