iT邦幫忙

1

【問題】Python selenium

  • 分享至 

  • xImage

造訪網站:https://e-service.cwb.gov.tw/HistoryDataQuery/MonthDataController.do?command=viewMain&station=C0A9C0&stname=%25E5%25A4%25A9%25E6%25AF%258D&datepicker=2018-01


我寫了一個程式,自動造訪該網頁後,
自動點擊【CSV下載】按鈕,
緊接著點擊下一頁的按鈕,
原本之前執行都沒有問題,
但是今天在run程式時,
nex_button.click()此行顯示錯誤訊息,
跑出此錯誤:

ElementClickInterceptedException: Message: element click intercepted: Element <a id="nexItem" href="#">...</a> is not clickable at point (655, 39). Other element would receive the click: <th colspan="5">...</th>
  (Session info: chrome=94.0.4606.61)

我查詢了相關網站的問題,
也修改了用id、name其他去find_elements,
但都出現一樣的錯誤,
不知道該如何修改?
奇妙的是downloadCSV不會有問題,
(註:原本兩個都是用find_elements_by_css_selector,也是只有nexItem出現錯誤)


完整程式碼:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import requests
from bs4 import BeautifulSoup
import pandas as pd

#安裝chromedriver,chromedriver存放位置
dr = webdriver.Chrome('C:/Users/User/chromedriver')

#造訪網頁連結
dr.get('https://e-service.cwb.gov.tw/HistoryDataQuery/MonthDataController.do?command=viewMain&station=C0A9C0&stname=%25E5%25A4%25A9%25E6%25AF%258D&datepicker=2018-01')

#自動點選【CSV下載】button
first_buttons = dr.find_elements_by_css_selector('a[id = downloadCSV]') 
#自動點選【下一個月箭頭】button
nex_buttons = dr.find_elements_by_xpath("/html/body/div[1]/table/tbody/tr/td[6]/a") 

for first_button in first_buttons:
    first_button.click()
    
for nex_button in nex_buttons:
    nex_button.click()

更新:
後來發現我只要將我的程式碼順序改變即可完成,
因為換頁時,容易抓不到id,

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import requests
from bs4 import BeautifulSoup
import pandas as pd

#安裝chromedriver,chromedriver存放位置
dr = webdriver.Chrome('C:/Users/User/chromedriver')

#造訪網頁連結
dr.get('https://e-service.cwb.gov.tw/HistoryDataQuery/MonthDataController.do?command=viewMain&station=C0A9C0&stname=%25E5%25A4%25A9%25E6%25AF%258D&datepicker=2018-01')

for first_button in first_buttons:
    #自動點選【CSV下載】button
    first_buttons = dr.find_elements_by_css_selector('a[id = downloadCSV]') 
    first_button.click()
 
for nex_button in nex_buttons:
    #自動點選【下一個月箭頭】button
    nex_buttons = dr.find_elements_by_xpath("/html/body/div[1]/table/tbody/tr/td[6]/a") 
    nex_button.click()
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
we95
iT邦新手 5 級 ‧ 2021-10-04 11:05:43
最佳解答
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time

#安裝chromedriver,chromedriver存放位置
dr = webdriver.Chrome('C:/Users/User/chromedriver')
#造訪網頁連結
dr.get('https://e-service.cwb.gov.tw/HistoryDataQuery/MonthDataController.do?command=viewMain&station=C0A9C0&stname=%25E5%25A4%25A9%25E6%25AF%258D&datepicker=2018-01')
dr.maximize_window()
#自動點選【CSV下載】button
first_buttons = dr.find_elements_by_css_selector('a[id = downloadCSV]') 
time.sleep(2)
#自動點選【下一個月箭頭】button
nex_buttons = dr.find_elements_by_xpath("/html/body/div[1]/table/tbody/tr/td[6]/a") 
time.sleep(2)

for first_button in first_buttons:
    first_button.click()
    
for nex_button in nex_buttons:
    nex_button.click()

試著將視窗最大化+設置時間讓網頁跑完

0
jiatool
iT邦研究生 3 級 ‧ 2021-10-02 10:05:52

我查到這個錯誤可能是有彈跳視窗擋住。

會不會是點選【CSV下載】button後出現儲存檔案的彈跳視窗擋住,導致無法點選【下一個月箭頭】button?
https://ithelp.ithome.com.tw/upload/images/20211002/20139617kQwYXw7Y60.png

看看這個能不能解決:https://www.itread01.com/content/1547280554.html

0
小魚
iT邦大師 1 級 ‧ 2021-10-02 12:45:22

如果是彈跳視窗擋住,
那就是檢查視窗出現的時候按確定,
檢查視窗消失的之後再按下一個月.
selenium有提供這方面的功能.

我要發表回答

立即登入回答