2016-03-30 187 views
0

我试图点击网页的一部分,但我收到消息“NoSuchElementException:无法找到元素”....尽管元素在那里。Python Selenium:找不到Xpath元素

该代码用于工作,但它看起来像页面发生了变化..但xpath没有改变。

我在这里尝试了Stackoverflow中类似问题的不同解决方案,但对于这个例子还是有些不正确。

的网址是: “http://www.bmfbovespa.com.br/pt_br/servicos/market-data/consultas/mercado-a-vista/codigo-isin/pesquisa/

元素我真的试图点击 “下载德Arquivos”

我的代码:

from selenium import webdriver 


fp = webdriver.FirefoxProfile() 
fp.set_preference("browser.download.folderList",2) 
fp.set_preference("browser.download.manager.showWhenStarting",False) 
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/vnd.ms-excel, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream") 
fp.set_preference('browser.helperApps.alwaysAsk.force', False) 



driver = webdriver.Firefox(firefox_profile=fp) 
driver.get("http://www.bmfbovespa.com.br/pt_br/servicos/market-data/consultas/mercado-a-vista/codigo-isin/pesquisa/") 

### 
# Click "Download de arquivos" (the part with problem) 
### 

elem=driver.find_element_by_xpath(".//*[@id='ctl00_contentPlaceHolderConteudo_rtsDetalhe_tabDownload']/span/span") 
elem.click() 

有什么想法?

回答

0

多种选择这里:

  • 定位 “的ID” 链接:

    driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_rtsDetalhe_tabDownload") 
    
  • 通过链接文本通过 “与XPath” 定位:

    driver.find_element_by_xpath("//a[span/span = 'Download de Arquivos']") 
    

而且,重要的部分是元素在iframe之内 - 你需要切换到它。

工作代码:

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

driver = webdriver.Firefox() 
driver.get("http://www.bmfbovespa.com.br/pt_br/servicos/market-data/consultas/mercado-a-vista/codigo-isin/pesquisa/") 

driver.switch_to.frame("bvmf_iframe") 

wait = WebDriverWait(driver, 10) 
elem = wait.until(EC.presence_of_element_located((By.ID, "ctl00_contentPlaceHolderConteudo_rtsDetalhe_tabDownload"))) 
elem.click() 
+0

我都尝试....但我仍然得到“NoSuchElementException异常:找不到元素”的消息。 – fmarques

+0

@fmarques好的,更新,检查出来。 – alecxe

+0

它的工作原理!非常感谢你! – fmarques