我试图在以下链接上单击并下载“ Real Sector”: http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm
这是我尝试过的:
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
driver.find_element_by_css_selector("a[href= 'Chap-2.pdf']").click()
except NoSuchElementException:
pass
但是它给出了以下错误:
ElementNotInteractableException:消息:无法将元素滚动到视图中
我该如何解决?
答案 0 :(得分:1)
您需要应用“显式等待”,并等待该元素出现在页面上,然后才能单击它。
您还可以先滚动到该元素,然后单击它。
您可以这样做:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[@href='Chap-2.pdf']")))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
except NoSuchElementException:
pass
OR
您可以使用Java脚本单击直接单击元素,例如:
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[@href='Chap-2.pdf']")))
driver.execute_script("arguments[0].click();", element)
except NoSuchElementException:
pass
答案 1 :(得分:0)
尝试等待使用element_to_be_clickable
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href= 'Chap-2.pdf']")))
element.click()
答案 2 :(得分:0)
要点击"Real Sector"
链接,请诱导WebDriverWait()
和element_to_be_clickable
()并遵循xpath
选项。
driver = webdriver.Chrome()
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'Real Sector')]"))).click()
您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
点击后浏览器快照。