简体   繁体   中英

Selenium: Can't click on a button within an iframe

I load a page with selenium: http://www.legorafi.fr/ Next I try to click on "Tout Accepter" button but even with the css selector it doesn't work. It is for the cookies.

I tried something like this:

driver.find_element_by_css_selector('').click()

This is the blue button with text "Tout Accepter"

在此处输入图片说明

How can I do ?

The element is present inside an iframe you need to switch the iframe in order to access the element.

Induce WebDriverWait () and wait for frame_to_be_available_and_switch_to_it () and following css selector

Induce WebDriverWait () and wait for element_to_be_clickable () and following xpath

driver.get("http://www.legorafi.fr/")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#appconsent>iframe")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='Tout Accepter']"))).click()

You need to import below libraries

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

The element Tout Accepter is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it .

  • Induce WebDriverWait for the desired element to be clickable .

  • You can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     driver.get('http://www.legorafi.fr/') WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div#appconsent>iframe"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button--filled>span.baseText"))).click()
  • Using XPATH :

     driver.get('http://www.legorafi.fr/') WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//div[@id='appconsent']/iframe"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'button--filled')]/span[contains(@class, 'baseText')]"))).click()
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:

莱戈拉菲


Reference

You can find a couple of relevant discussions in:

First switch to the banner frame, then click the accept button:

from selenium import webdriver

url = "http://www.legorafi.fr/"
driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame(2)
button = "/html/body/div/div/article/div/aside/section[1]/button"      
driver.find_element_by_xpath(button).click()

(I click the button using XPath but that's just personal taste)

Hope it helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM