使用python的selenium庫自動填寫網頁(疫情每日一報)


每日一報這種東西,上有政策,下就一定有對策。比如我就寫了個python程序實現了自動登錄填表,由於眾所周知的原因,這里略去具體機構和網頁,只分享代碼。

首先需要安裝selenium 依賴,並且還需要下載一個webdriver,我使用的是chrome的webdriver。

https://chromedriver.chromium.org/downloads

直接上代碼:

 1 from selenium import webdriver
 2 import time
 3 
 4 from selenium.common.exceptions import NoSuchElementException
 5 
 6 driver = webdriver.Chrome('./chromedriver')
 7 driver.get("http://XXXXXXX.XXXX.XXX.cn")
 8 
 9 account = driver.find_element_by_id("username")
10 account.send_keys("1XXXXXXX")
11 
12 pwd = driver.find_element_by_id("password")
13 pwd.send_keys("0XXXXXXXXX")
14 
15 login = driver.find_element_by_id("login-submit")
16 login.click()
17 time.sleep(3)
18 try:
19     messageNotification = driver.find_element_by_id("layui-layer1")
20 except NoSuchElementException:
21     print ("No new messages")
22 else:
23     confirm = driver.find_element_by_class_name("layui-layer-btn0")
24     confirm.click()
25     try:
26         while True:
27             driver.find_element_by_css_selector('[style="color:red;"]').find_element_by_xpath('..').click()
28             rtn = driver.find_element_by_css_selector('[role="button"]')
29             rtn.click()
30     except NoSuchElementException:
31         print ("No unread message found!")
32 
33 
34 try:
35     while True:
36         x = driver.find_element_by_id("fineui_1")
37         x.click()
38         reportHis = driver.find_element_by_id("lnkReportHistory")
39         reportHis.click()
40 
41         toWrite = driver.find_element_by_css_selector('[href^="/DayReport.aspx"]')
42         toWrite.click()
43         checkBox = driver.find_element_by_id("p1_ChengNuo-inputEl-icon")
44         checkBox.click()
45         temperature = driver.find_element_by_id("p1_TiWen-inputEl")
46         temperature.send_keys("36")
47         submit = driver.find_element_by_id("p1_ctl00_btnSubmit")
48         submit.click()
49         subConf = driver.find_element_by_id("fineui_68")
50         subConf.click()
51         time.sleep(3)
52         subConfConf = driver.find_element_by_id("fineui_73")
53         subConfConf.click()
54 except NoSuchElementException:
55     print("All reported. Exiting...")
56 
57 driver.close()

先大體解釋一下,9-17行進行登錄操作,18-31行檢查消息中心的未讀消息並全部自動讀取,34-55行進入每日一報,自動填寫所有未填的表單。

技術上值得注意的一些地方:

  1.  有時,代碼邏輯沒有問題,但卻出現類似 stale element reference: element is not attached to the page document 這樣的錯誤,也就是webdriver找不到我們指定的元素,很有可能是因為網頁還沒完全加載,程序就對其進行操作。使用time.sleep() 讓程序等待一下網頁,一般就能解決。
  2. 無論是find_element_by 還是find_elements_by ,如果沒有找到對應的元素,拋出一個NoSuchElementException 異常。
  3. 定位到父節點元素的方法:
    find_element_by_xpath('..')
  4. 使用css selector 以屬性定位元素的方法:
    driver.find_element_by_css_selector('[style="color:red;"]')
  5. 用類似於正則的屬性通配符,對屬性值進行模糊匹配:
    find_element_by_css_selector('[href^="/DayReport.aspx"]')  參考:https://www.w3.org/TR/selectors/#attribute-substrings

6.2. Substring matching attribute selectors

Three additional attribute selectors are provided for matching substrings in the value of an attribute:

[att^=val]
Represents an element with the  att attribute whose value begins with the prefix "val". If "val" is the empty string then the selector does not represent anything.
[att$=val]
Represents an element with the  att attribute whose value ends with the suffix "val". If "val" is the empty string then the selector does not represent anything.
[att*=val]
Represents an element with the  att attribute whose value contains at least one instance of the substring "val". If "val" is the empty string then the selector does not represent anything.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM