I can't find elements by Id inside an HTML source page using Selenium

Question:

I am trying to get a list at the website, clicking on a button (‘Todas’). The Todas button Id at the browser HTML source and my Python code are:

Button Id:'ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_btnTodas'

    from selenium import webdriver
    import time

    driver = webdriver.Firefox(executable_path='')
    driver.implicitly_wait(12)
    driver.get("http://www.bmfbovespa.com.br/pt_br/produtos/listados-a-vista-e-derivativos/renda-variavel/empresas-listadas.htm")

    driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_btnTodas")

Error message:

NoSuchElementException: Unable to locate element:
{"method":"id","selector":"ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_btnTodas"}

In fact, the element is present in the browser HTML.

https

I read the related topics, but I didn’t get a solution.

So, what do I need to do in order to click that button and get the data list after?

Answers:

As I’m seeing in your provided website, this TODAS button is inside an iframe with id bvmf_iframe. You need to switch that frame before finding this button as below:

driver = webdriver.Firefox(executable_path='')
driver.implicitly_wait(12)
driver.switch_to_frame("bvmf_iframe")
driver.get("http://www.bmfbovespa.com.br/pt_br/produtos/listados-a-vista-e-derivativos/renda-variavel/empresas-listadas.htm")

driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_btnTodas")
Answered By: Saurabh Gaur

The reason is because this page is using an iFrame. You will need to switch to the iframe before attempting to find your element:

iframe = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_default_content()

driver.switch_to_frame(iframe)
driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_btnTodas")
Answered By: Moe Ghafari
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.