Home > Net >  Selenium Error: Element not interactable (coockie and other pop up)
Selenium Error: Element not interactable (coockie and other pop up)

Time:10-16

I am trying to press a button with selenium because afterwards I need to inspect the full html of the website. This is the code that I am using:

driver = webdriver.Chrome()
driver.get('https://www.quattroruote.it/listino/audi/a4-allroad')
time.sleep(10)
html = driver.find_element_by_id('btnallestimenti')
html.click()

But I get this error: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

when the page is open there are cookies and other things that shows up, is there a way to block all of them so that I can work on the html?

Thanks a lot!

CodePudding user response:

You could try to accept the cookie and proceed further, check the below lines of code.

options = Options()
options.add_argument("--disable-notifications")


driver = webdriver.Chrome(options=options,"ChromeDriver_Path")
driver.maximize_window()
driver.get('https://www.quattroruote.it/listino/audi/a4-allroad')
sleep(10)

cookie_btn = driver.find_element_by_xpath("//button[text()='Accetta']")
cookie_btn.click()
sleep(3)

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"promo-premium-iframe")))
register_btn = driver.find_element_by_xpath("//a[normalize-space()='Accedi o Registrati']")
register_btn.click()

Iframe available so just switched to the iframe, tried the to perform the registration.

CodePudding user response:

As you can see the "cookies" banner is an HTML element itself and it contains a "Close" ("Chiudi") button that can be clicked.

If you inspect the page source you will find this code that relates to that button:

<button type="button" class="iubenda-cs-close-btn" tabindex="0" role="button" aria-pressed="false" style="font-size:16px!important;">Chiudi</button>

Your script needs to be modified to search the element by visible text (using the XPath) and click it to close the banner:

close_button = driver.find_element_by_xpath("//*[text()='Chiudi']")

close_button.click()

I can see that this kind of banner appears 2 times (one for cookies one for "Informativa") but once you click this one away you are redirected to the right page.

Of course you will need to test your script and adjust it to the page's behavior.

Also, be aware that every time the pages change because the devs change it your script will break and you will need to re-adjust it.

EDIT

Posting here the full code, try to use it and continue from here:

import time
from selenium.webdriver import Chrome

driver = Chrome()

driver.get("https://www.quattroruote.it/listino/audi/a4-allroad")
time.sleep(6)

driver.find_element_by_xpath("//button[text()='Accetta']").click()
time.sleep(6)

driver.switch_to.frame("promo-premium-iframe")
driver.find_element_by_xpath("//a[normalize-space()='Non sono interessato']").click()
time.sleep(6)

driver.switch_to.default_content()

driver.find_element_by_id("btnallestimenti").click()

input()
  • Related