我正试图从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 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.implicitly_wait(30)
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"]")))
#select date range picker
element = 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()
我遇到的错误如下
无法定位元素:{quot;方法":"xpath"选择器"会话信息:chrome=81.0.4044.113(
我似乎不知道到底是什么不起作用——我使用chrome中的开发工具检查元素,复制了xpath。
日期选择器元素存在于iframe
中。您需要首先切换iframe
才能访问日期选择器。
诱导WebDriverWait
((并等待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()