【发布时间】:2022-01-19 10:41:58
【问题描述】:
尝试单击带有Selenium 的按钮,但我不断收到错误消息:
NoSuchElementException:消息:没有这样的元素:无法定位 元素:{“方法”:“链接文本”,“选择器”:“同意”}
这是我要点击的按钮。
我假设,弹出窗口是延迟加载的。我找到了一些来源,但无法使其工作。这是我的代码
import pandas as pd
import bs4
import selenium as sel
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
import os
driver = sel.webdriver.Chrome() #uses Chrome driver in usr/bin/ from https://chromedriver.chromium.org/downloads
url = 'https://fbref.com/en/squads/0cdc4311/Augsburg'
driver.get(url)
time.sleep(5)
html = driver.page_source
soup = bs4.BeautifulSoup(html, 'html.parser')
#click button -> accept cookies
element = driver.find_element(By.LINK_TEXT, "AGREE")
element.click()
>>> NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"AGREE"}
我也试过
[...]
driver = sel.webdriver.Chrome() #uses Chrome driver in usr/bin/ from https://chromedriver.chromium.org/downloads
driver.implicitly_wait(10) # seconds'
[...]
并且弹出窗口确实存在。但仍然得到同样的错误。
【问题讨论】:
-
我看那里没有接受 cookies 元素。
-
Mhhh,可能是一些地理特定问题要重现?我添加了屏幕截图和缺少的代码行。
标签: python selenium web-scraping