我需要使用 Python 自动下载此网页中的 .csv 文件:
https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie
现在,我写了这段代码:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
chromedriver_path = r"./driver/chromedriver"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie"
topics_xpath = '//*[@id="challenge-stage"]/div/label/span[2]'
browser.get(url)
time.sleep(5) #Wait a little for page to load.
escolhe = browser.find_element("xpath", topics_xpath)
time.sleep(5)
escolhe.click()
time.sleep(5)
网页打开,然后提示我单击“验证您是人类”:
我已经“检查”了按钮并复制了 xpath(参见上面的代码)。
但我收到这个错误:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="challenge-stage"]/div/label/span[2]"}
(Session info: chrome=114.0.5735.198)
有人可以帮我吗?
The checkbox与文本相关的验证你是人类元素在一个<iframe>所以你必须:
-
Induce WebDriver等待为了想要的框架可用并切换到它.
-
Induce WebDriver等待为了想要的元素可点击.
-
您可以使用以下任一方法定位策略:
-
Using CSS_选择器:
driver.get("https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie")
time.sleep(5)
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Widget containing a Cloudflare security challenge']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.ctp-checkbox-label"))).click()
-
Using XPATH:
driver.get("https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie")
time.sleep(5)
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='Widget containing a Cloudflare security challenge']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@class='ctp-checkbox-label']"))).click()
-
Note:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考
您可以在以下位置找到一些相关讨论:
- 通过 Selenium 和 python 切换到 iframe
- selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:尝试使用 selenium 单击“下一步”按钮时无法找到元素
- python 中的selenium:NoSuchElementException:消息:没有这样的元素:无法找到元素
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)