Home > Back-end >  Can not find by xpath until inspect the element
Can not find by xpath until inspect the element

Time:03-08

I was using Selenium find_element_by_xpath to click the cookie popup "Accept" button. But it was failing to find the button. So, I tried to locate the button using querySelector in the dev console. Also, the querySelector failed to find the button but after clicking inspect on the button the querySelector able to find the button.

Also, searching the xpath in dev elements just showing 1 of 1.

Why this is happening ? How do I click the "Accept" button using selenium ?

Website link: can not locate the button

After inspect on the button.

can locate the button

CodePudding user response:

That element is inside an iframe, so to access it you will first have to switch to that iframe.
You didn't mention what language are you using so I will give my solution in Python. This can be done with other languages as well with some little syntax changes.

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

wait = WebDriverWait(driver, 20)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'iframe[title="SP Consent Message"]')))
wait.until(EC.visibility_of_element_located((By.XPATH, '//button[@title="ACCEPT ALL"]'))).click()

When finished working inside this iframe you will have to switch back to the default content with

driver.switch_to.default_content()
  • Related