亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

似乎無法使用 selenium 通過 XPATH 選擇日期范圍選擇器

似乎無法使用 selenium 通過 XPATH 選擇日期范圍選擇器

富國滬深 2024-01-22 14:23:18
我正在嘗試從http://covid.gov.pk/stats/pakistan抓取數據。我希望腳本能夠單擊日期范圍選擇器來更改日期,但我似乎無法選擇它,我正在使用的 XPATH 如下。//*[@id="body"]/div/div/div[1]/div[2]/div/div[1]/div[1]/div[1]/div/lego-report/lego-canvas-container/div/file-drop-zone/span/content-section/canvas-component[66]我正在使用的Python腳本from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.implicitly_wait(30)driver.get("http://covid.gov.pk/stats/pakistan")#wait for Page to loadWebDriverWait(driver, 30, ).until(EC.invisibility_of_element((By.XPATH, "//div[@id=\"preloader\"]")))#select date range pickerelement = driver.find_element_by_xpath("//*[@id=\"body\"]/div/div/div[1]/div[2]/div/div[1]/div[1]/div[1]/div/lego-report/lego-canvas-container/div/file-drop-zone/span/content-section/canvas-component[66]")element.click()我遇到的錯誤如下無法定位元素:{"method":"xpath","selector":"//*[@id="body"]/div/div/div[1]/div[2]/div/div[1 ]/div[1]/div[1]/div/lego-report/lego-canvas-container/div/file-drop-zone/span/content-section/canvas-component[66]"} (會話信息:鉻=81.0.4044.113)我似乎無法弄清楚到底是什么不起作用,我通過使用 chrome 中的開發人員工具檢查元素來復制 xpath。
查看完整描述

1 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

日期選擇器元素存在于 . 中iframe。您需要切換iframe第一個才能訪問日期選擇器。


Induce WebDriverWait() 和 wait for frame_to_be_available_and_switch_to_it() 并使用以下 css 選擇器。


然后您可以使用以下 xpath 單擊日期選擇器。


from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()

driver.get("http://covid.gov.pk/stats/pakistan")

#wait for Page to load

WebDriverWait(driver,30).until(EC.invisibility_of_element((By.XPATH, "//div[@id='preloader']")))

WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,".pak-stats-ifrm")))

#select date range picker

element = driver.find_element_by_xpath("//div[@class='content-holder ng-scope']")

element.click()


查看完整回答
反對 回復 2024-01-22
  • 1 回答
  • 0 關注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號