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

Simple use of automated Python

編輯:Python

List of articles

Environment configuration

  1. pycharm  Environment configuration
  2. download  chrome drives.exe , Version selection to be compared with chrome The browser version is similar to python Catalog  script  Next
  3. stay pycharm Enter the following code to make python coordination seninum Realize automatic input

1.Link_text Locate hyperlinks

from selenium import webdriver
# Create a browser
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# according to linktext Method to locate the words in the hyperlink attribute interface " Journalism ", See if you can open the news hyperlink ( Be careful link_text The value is in single quotation marks )
#driver.find_element_by_link_text(' Journalism ').click()
# according to partial_linktext Method to locate the fuzzy words in the hyperlink attribute interface " The earth ", See if you want to open the map hyperlink
driver.find_element_by_partial_link_text(' The earth ').click()
import time
# The purpose of deep sleep is to make the program slow and easy to watch , Sometimes it's waiting for the element to render
time.sleep(5)
# Close the browser
#driver.quit()

2. Mixed element positioning

a. Attribute positioning method

# Find all id=root The elements of
driver.find_element_by_xpath('//*[@id="root"]

b. Find all id Value inclusion "con" All elements of

 notes :contains() Is a function
//*[contains(@id,"con")]

c. Find all that contain id=footerLayer And contain class=relativeFooterLayer All elements of

//*[@id="footerLayer" and @class="relativeFooterLayer" ]

d. Find all that contain id=footerLayer perhaps contain class=relativeFooterLayer All elements of

/*[@id="footerLayer" or @class="relativeFooterLayer" ]

f. lookup class With mod At the beginning div attribute

//div[starts-with(@class,'mod')]

3.Xpath location ( Usually )

1. Absolute positioning

Find the corresponding elements from the top layer to the bottom layer

shortcoming : Page changes , The absolute address will change , Will be positioned incorrectly

from selenium import webdriver
import time
# Create a browser
driver = webdriver.Chrome()
driver.get("https://www.ctrip.com/")
# Use xpath Method absolute positioning click Ctrip's strategy . Scenic spot function
driver.find_element_by_xpath('/html/body/div/div[@id="leftSideNavLayer"]/div/div/div[@style]/div/div/div/div[6]').click()
# The purpose of deep sleep is to make the program slow and easy to watch , Sometimes it's waiting for the element to render
sleep(2)
# Close the browser
driver.quit()
  1. Relative positioning ( often )
from selenium import webdriver
import time
# Create a browser
driver = webdriver.Chrome()
driver.get("https://www.ctrip.com/")
# Use xpath Methods fuzzy positioning click Ctrip's strategy . Scenic spot function
driver.find_element_by_xpath('//body//div[@id="root"]//div[@class="lsn_first_nav_wrap_LZamG"][5]').click()
# The purpose of deep sleep is to make the program slow and easy to watch , Sometimes it's waiting for the element to render
sleep(2)
# Close the browser
driver.quit()

4.css location

Be careful :

1. Spaces are elements that span one or more levels

2.> Indicates the lower sub label

3. attribute :nth-child(1) , Represents the first of the same attributes

  1. # Express id

     Find all id=ie-update The elements of
    #ie-update
  2. . Express class

 Find all class=lsn_first_nav_wrap_LZamG The elements of
.lsn_first_nav_wrap_LZamG

In order to locate   millet note 11T pro+  The price of

# Indicates positioning class The value is brick-list clearfix Of ul Under the label li label , Across multiple levels class be known as price Of p label , Under the span label
ul[class="brick-list clearfix"]>li p.price>span

1. Realize automatic input in the input box

# This action will automatically open the browser , Baidu website , Input " Are you all right? "," How are you? "," How was your day , Is it substantial ?" One of them , And the loading time will slow down by five seconds , After completing the above results , Will automatically close the page
from selenium import webdriver
# Create a browser
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# according to id Attribute positioning element
#driver.find_element_by_id('kw').send_keys(" Are you all right? ")
# according to class Attribute positioning element
#driver.find_element_by_class_name('s_ipt').send_keys(" How are you? ")
# according to name Attribute positioning element
driver.find_element_by_name('wd').send_keys(" How was your day , Is it substantial ?")
import time
# The purpose of deep sleep is to make the program slow and easy to watch , Sometimes it's waiting for the element to render
time.sleep(5)
# Close the browser
driver.quit()

2. Clear the input box

from selenium import webdriver
import time
# Create a browser , Open Baidu input box
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
# Defining variables th For the input box
th=driver.find_element_by_id('kw')
# Enter text in the input box , Empty , Input again
time.sleep(3)
th.send_keys(" Endless words .....")
time.sleep(1)
th.clear()
th.send_keys(" Are you all right? ")
time.sleep(2)
# Close the browser
driver.quit()

3. Upload files

Baidu automatically uploads pictures to search

from selenium import webdriver
import time
# Create a browser
driver=webdriver.Chrome()
# Enter Baidu website in the browser
driver.get("https://www.baidu.com/")
# Click the upload button
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/span[1]').click()
# Click to select file
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[5]/div/div/form/div/div[2]/div[2]/input').send_keys('C:\\Users\\hhh\\Desktop\\ Baidu element .png')
time.sleep(6)
driver.quit()

4. Automate execution javaScript Method

stay f12 Next console Enter the following code

# Because the date is different every day of each month , So we introduced document.querySelector() Method
# Search for class be known as td In the label of id The value is "June-29-2022"
# So if you want to click on another date , Direct change id It's worth it
document.querySelector('[class="pi-input J_DepDate has-trans trigger-node-406"]').placeholder="2022-3-15"

stay python perform javaScript Format

driver.execute_script('document.querySelector(\'[class="pi-input J_DepDate has-trans trigger-node-406"]\').placeholder="2022-3-15"')

5. Browser window switch

from selenium import webdriver
import time
# Create a browser , Open Baidu input box
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
# Search the input box for
driver.find_element_by_id('kw').send_keys("hello,world")
time.sleep(2)
# Click on the search
driver.find_element_by_id('su').click()
# Click on the Baidu Encyclopedia of the text
driver.find_element_by_xpath('//div[@class="bk_polysemy_1Ef6j"]//a').click()
time.sleep(5)
# Get all browser windows
windows=driver.window_handles
# Select the second new browser window
driver.switch_to.window(windows[-1])
# Switch to the first browser window
driver.switch_to.window(windows[0])

6. Switch ifame

Why switch ifame

The reason is that a web page is nested in a web page , Don't switch ifame, Cannot locate element

from selenium import webdriver
import time
# Create a browser , Open the alicloud registration interface
driver = webdriver.Chrome()
driver.get("https://account.aliyun.com/register/qr_register.htm")
# Define a variable frame , The value is the positioning frame Elements
frame = driver.find_element_by_id('alibaba-register-box')
# Cut to html Next frame page
driver.switch_to.frame(frame)
# Enter the account number in the account number column of the registration interface
driver.find_element_by_xpath('//div[@class=" next-form-item-control"]//input').send_keys("17655870668")
# Enter the password in the account column of the registration interface
driver.find_element_by_xpath('//div[@class="pwd-wrap"] //input[@type="password"]').send_keys("edwdaf")
# Switch to... Again frame Outer layer
driver.switch_to.parent_frame()
time.sleep(3)
driver.quit()

7.alert Window switch

from selenium import webdriver
import time
# Create a browser
driver = webdriver.Chrome()
# Open my local pop-up window html file
driver.get("E:\\python_pro\\venv\\1.html")
# Define a variable for the pop-up alert window
alert = driver.switch_to.alert
print(alert.text)
time.sleep(3)
driver.quit()

8. Simulate mouse operation

from selenium import webdriver
import time
# introduce ActionChains class
from selenium.webdriver.common.action_chains import ActionChains
# Create a browser
driver = webdriver.Chrome()
# Open the JD interface
driver.get("https://www.jd.com")
time.sleep(3)
# Define a variable m Used to receive " Website navigation " location
m = driver.find_element_by_xpath('//*[@aria-label=" Website navigation "]')
# Passing in variables m, And hover the mouse over " Website navigation "
ActionChains(driver).move_to_element(m).perform()#perform() Method means execution
time.sleep(3)
# Define a variable x Used to receive " Jingdong communications " location
x = driver.find_element_by_xpath('//a[@role="menuitem" and text()=" Jingdong communications "]')
# Passing in variables x, And hover the mouse over " Website navigation " Of " Jingdong communications " Subpage
ActionChains(driver).move_to_element(x).click().perform()
time.sleep(3)
driver.quit()

9. Simulate drag and drop operation ?

from selenium import webdriver
import time
# introduce ActionChains class
from selenium.webdriver.common.action_chains import ActionChains
# Create a browser
driver = webdriver.Chrome()
action= ActionChains(driver)
# Open the map interface
driver.get("https://map.baidu.com/@11590057.96,4489812.75,4z")
time.sleep(6)
time.sleep(3)
# Location map location
u=driver.find_element_by_xpath('//*[@type="system"]')
# move_to_element(mask) Move to this element ,click_and_hold() Press mouse ,move_by_offset(50,30) Offset drag ,release() Loosen the mouse ,perform() Perform the operation
action.move_to_element(u).click_and_hold().move_by_offset(50,30).release().perform()
time.sleep(3)
driver.quit()

10. An implicit wait ?

In order to solve the problem that the page pops up the login window for a period of time

from selenium import webdriver
import time
# introduce ActionChains class
from selenium.webdriver.common.action_chains import ActionChains
# Create a browser
driver = webdriver.Chrome()
# Implicitly waiting for page element time (0-15s)
driver.implicitly_wait(15)
# Open the map interface
driver.get("https://map.baidu.com/@11590057.96,4489812.75,4z")
# Record the start time
start_time = time.perf_counter()
# Click the account number to enter
driver.find_element_by_xpath('//*[@name="userName"]').send_keys('1321454654')
# Click password input
driver.find_element_by_xpath('//*[@name="password"]').send_keys('zawwe343234')
# Click login
driver.find_element_by_xpath('//*[@value=" Sign in "]').click()
action= ActionChains(driver)
# Location map location
u=driver.find_element_by_xpath('//*[@type="system"]')
# move_to_element(mask) Move to this element ,click_and_hold() Press mouse ,move_by_offset(50,30) Offset drag ,release() Loosen the mouse ,perform() Perform the operation
action.move_to_element(u).click_and_hold().move_by_offset(50,30).release().perform()
time.sleep(3)
driver.quit()

11. According to wait ?

You can control the waiting time of a single element

from selenium import webdriver
import time
# The display wait function is introduced
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.by import By
# Create a browser
driver = webdriver.Chrome()
# Open the map interface
driver.get("https://map.baidu.com/@11590057.96,4489812.75,4z")
# Record the start time
#start_time = time.perf_counter()
WebDriverWait(driver,5).until(EC.presence_of_element_located(By.CSS_SELECTOR,'//*[@name="userName"]'))
# Record the end time
#end_time = time.perf_counter()
#print(f'{end_time - start_time} s')
# Click the account number to enter
driver.find_element_by_xpath('//*[@name="userName"]').send_keys('345761231')
# Click password input
driver.find_element_by_xpath('//*[@type="password" and @name="password"]').send_keys('21323dwe')
# time.sleep(3)
# driver.quit()

1. Auto fill login csdn

from selenium import webdriver
import time
# Create a browser
driver=webdriver.Chrome()
# Browser input csdn website
driver.get("https://www.csdn.net/?spm=1000.2115.3001.4476")
# Find hyperlink text login / register , And then click
driver.find_element_by_link_text(' Sign in / register ').click()
# Click login mode - Password to login
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/div[1]/div[1]/div[1]/span[4]').click()
# Input csdn account number
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/div[1]/div/div[2]/div/div[1]/div/input').send_keys(' account number ')
# Input csdn password
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/div[1]/div/div[2]/div/div[2]/div/input').send_keys(' password ')
# Sign in
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/div[1]/div/div[2]/div/div[4]/button').click()
time.sleep(10)
driver.quit()

2. Implicit login Baidu map ?

from selenium import webdriver
import time
# introduce ActionChains class
from selenium.webdriver.common.action_chains import ActionChains
# Create a browser
driver = webdriver.Chrome()
# Implicitly waiting for page element time (0-15s)
driver.implicitly_wait(5)
# Open the map interface
driver.get("https://map.baidu.com/@11590057.96,4489812.75,4z")
# Record the start time
#start_time = time.perf_counter()
# Click the account number to enter
driver.find_element_by_xpath('//*[@name="userName"]').send_keys('423425465')
# Click password input
driver.find_element_by_xpath('//*[@type="password" and @name="password"]').send_keys('8vsdrw5')
# Click login
driver.find_element_by_xpath('//*[@value=" Sign in " and @type="submit"]').click()
# Record the end time
#end_time = time.perf_counter()
#print(f'{end_time - start_time} s')
action = ActionChains(driver)
# Location map location
u=driver.find_element_by_xpath('//*[@type="system"]')
#move_to_element(mask) Move to this element ,click_and_hold() Press mouse ,move_by_offset(50,30) Offset drag ,release() Loosen the mouse ,perform() Perform the operation
action.move_to_element(u).click_and_hold().move_by_offset(50,30).release().perform()
# time.sleep(3)
# driver.quit()

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