Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can not find by xpath until inspect the element

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Website link: https://www.transfermarkt.com/

The xpath: //*[@id="notice"]/div[3]/div[2]/button

can not locate the button

After inspect on the button.

can locate the button

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading