繁体   English   中英

Python Selenium:找不到Xpath元素

[英]Python Selenium : Xpath element not found

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

该代码曾经可以工作,但是页面似乎发生了变化。.但是xpath并没有变化。

我在Stackoverflow中尝试了类似问题的不同解决方案,但对于该示例而言,尚不正确。

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

我尝试点击的元素:“下载de 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()

有什么想法吗?

这里有多个选项:

  • 找到链接“按ID”:

     driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_rtsDetalhe_tabDownload") 
  • 通过“ by 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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