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

How to click a button in html within an iframe using Selenium

I need to extract some values from a website, and to do so, I have to click on a button on that page. However, there seems to be an issue. I noticed that when I hit F12, I can identify the specific tag, class, XPath, and so forth, but when I use "CTRL+U" and attempt to find that button, it’s not visible. I’m not sure why this happens, but okay.

This is the website:

https://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm

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

When I execute the following code:

driver.find_element(By.XPATH, '/html/body/app-root/app-companies-home/div/div/div/div/div[1]/div[2]/div/app-companies-home -filter-name/form/div/div[4]/button').click()

Essentially, I’m trying to simulate a click on the button labeled "TODOS",
whose tag is:

<button type="submit" aria-label="Buscar Todas" class="btn btn-light btn-block mt-3">Todos</button>

and the FULL XPATH path is:

/html/body/app-root/app-companies-home/div/div/div/div/div[1]/div[2]/div/app-companies-home-filter-name/form/div/div[4]/button

However, I receive this error message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/app-root/app-companies-home/div/div/div/div/div[1]/div[2]/div/app-companies-home-filter-name/form/div/div[4]/button"}

It seems that the HTML element isn’t being found, despite its visibility when pressing F12. Any advice on how to resolve this issue?

This is my full code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--remote-debugging-port=9222")

driver = webdriver.Chrome(service=None, options=chrome_options)
driver.get(
    "https://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm"
)
time.sleep(5)
driver.find_element(
    By.XPATH,
    "/html/body/app-root/app-companies-home/div/div/div/div/div[1]/div[2]/div/app-companies-home-filter-name/form/div/div[4]/button",
).click()

I tried to use other ways to use find_element() but without success

>Solution :

The element Todos 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("https://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm")
      WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
      WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#bvmf_iframe")))
      WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[aria-label='Buscar Todas']"))).click()
      
    • Using XPATH:

      driver.get("https://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm")
      WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
      WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='bvmf_iframe']")))
      WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//button[@aria-label='Buscar Todas']"))).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:

Todos


Reference

You can find a couple of relevant discussions in:

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