How to locate element inside IFRAME with Selenium Python?

Question:

I can’t find the search button on the screen below:
type image description here

I noticed that it is inside an IFRAME in the HTML

I used some methods to click the button that didn’t work, it always returns the error message that the element is not found with the XPATH.

follows the elements that I tried to click and commands

driver.find_element(By.XPATH"//div[contains(text(),'Search')]").click()

wait.until(EC.element_to_be_clickable((By.XPATH,"//div[contains(text(),'Search')]"))).click()

driver.find_element(By.XPATH,"/html[1]/body[1]/div[2]/div[1]/div[3]/div[2]/div[1]/div[1] /div[1]/div[1]/div[2]").click()

Asked By: Felipe

||

Answers:

First, you need to switch to that iframe with something like this:

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='blogger.com']")))

Then, inside the iframe you should be able to access that element with code like this:

wait.until(EC.element_to_be_clickable((By.XPATH,"//div[contains(text(),'Pesquisa')]"))).click()

In case there are nested iframes, you need to switch to first iframe first, then to the inner iframe.
I can’t give you working locators since I can’t access the page you working on.
After you finished working inside the iframe you wil need to switch to the default content with

driver.switch_to.default_content()
Answered By: Prophet