0

Trying to click a button with Selenium, but I keep getting an error:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"AGREE"}

Here's the button I am trying to click.

enter image description here

I assume, the popup is loaded lazy. I found some sources, but could not make it work. Here's my code

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"}

I also tried

[...]
driver = sel.webdriver.Chrome() #uses Chrome driver in usr/bin/ from https://chromedriver.chromium.org/downloads
driver.implicitly_wait(10) # seconds'
[...]

and the popup is definitively there. But still get the same error.

2
  • I see no accept cookies element there. Commented Dec 16, 2021 at 10:30
  • 1
    Mhhh, probably some geographical specific problem to reproduce? I added a screenshot and a missing line of code. Commented Dec 16, 2021 at 10:53

1 Answer 1

1

What happens?

You try to select a <button> via .LINK_TEXT, "AGREE", what won't work, cause it is a <button> not a link.

How to fix?

Wait for the <button> selected by xpath to be clickable:

#click button -> accept cookies
element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//button[text()="AGREE"]')))
element.click()
Sign up to request clarification or add additional context in comments.

4 Comments

Found out a couple of minutes ago. But thanks anyway.
Would you help me one more time? If I do element = driver.find_element(By.XPATH, '//button[text()="Get table as CSV (for Excel)"]') and element.click() I get Message: element not interactable
Of course SO is happy to help you with this, but to keep question / answer clean - This would be predestined for asking a new question Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.