简体   繁体   中英

I can't find elements by Id inside html source page Selenium

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 I need to do in order to click that button and get the data list after?

Thanks so much!

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")

Reason is because this page is using an iFrame, you will need to swicth 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM