0

I want to access an website with selenium and than a addblock-window appears, in which i need to click a button for it to disappear. adblock window

Eventhough I can find my XPath(//button[@title='Einverstanden'], /html/body/div/div[2]/div[3]/div[1]/button[@title = 'Einverstanden'] or //button[contains(text(),"Einverstanden")]') in the browser, Xpath in Browser

I can't find it with my Python script. And i can't seem to find the mistake.

Here is my code.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver  = webdriver.Firefox()

driver.implicitly_wait(30)

driver.get("``https://www.derstandard.at/story/2000134260361/endspiel-vor-gericht-prozess-gegen-boris-becker-startet-in-london``")
driver.maximize_window()
x = driver.find_element(By.XPATH, "//button[@title = 'Einverstanden']")
print(x)

This is the error I'm getting.

enter image description here

Prophet
  • 32,350
  • 22
  • 54
  • 79
Damiano
  • 3
  • 3

1 Answers1

0

The button is enclosed in an iframe in which case, you first need to switch to the iframe and then access the element

This should work:

driver.get("https://www.derstandard.at/consent/tcf/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[contains(@id, 'sp_message_container')]//iframe")))
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[title='Einverstanden']"))).click()

Wait Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

In case you want to switch to default frame again, you may use this when required:

driver.switch_to.default_content()
Anand Gautam
  • 2,018
  • 1
  • 3
  • 8
  • May i ask why you used the function .visibility_of_element_located(By.CSS_selector and not XPAth? – Damiano Mar 22 '22 at 13:34
  • You may use XPATH as well, no issues. I just picked it up the first thing that came to mind. It's a highly debatable topic that css_selector is optimzed than xpath though, but nevermind. XPATH still works and I mostly use XPATH. XPATH for the same element would be: `//button[@title='Einverstanden']` – Anand Gautam Mar 22 '22 at 13:52