嘗試單擊帶有 的按鈕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'
[...]
并且彈出視窗肯定存在。但仍然得到同樣的錯誤。
uj5u.com熱心網友回復:
怎么了?
您嘗試選擇一個<button>via .LINK_TEXT, "AGREE",這是行不通的,因為它<button>不是鏈接。
怎么修?
等待<button>選定的xpath可點擊:
#click button -> accept cookies
element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//button[text()="AGREE"]')))
element.click()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382728.html
