'Unable to locate element in Python Selenium with Chrome extension

I am trying to click on a button however when I do so I get an error stating that it cannot be found.

NOTE: You will only see this button if you have the chrome extension installed: enter image description here

Here's what I've tried:

chrome_options = Options()
chrome_options.add_extension("extension.crx") # Adds the downloaded extension

driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get("https://google.com/recaptcha/api2/demo")

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='recaptcha-anchor']"))).click() # Clicks on the recaptcha button

driver.switch_to_default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "solver-button"))).click() # Tries to click the special button provided by extension but fails

The error:

Unable to locate element/timeout exception

Website + source snippet: image

Why is it unable to locate the button and how can I fix it?



Solution 1:[1]

I believe that code is in an iframe. You likely need to switch to that frame to select from it's DOM:

WebDriver driver = new ChromeDriver();
driver.get("URL"); // URL OF WEBPAGE HAVING FRAMES

//First finding the element using any of locator
stratedgy WebElement iframeElement = driver.findElement(By.id("iframeResult")); 

//now using the switch
command driver.switchTo().frame(iframeElement);
driver.quit();

See also: https://www.browserstack.com/guide/handling-frames-in-selenium

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Quan