【问题标题】:Finding xpaths on pages running script在运行脚本的页面上查找 xpath
【发布时间】:2017-10-02 08:58:06
【问题描述】:

我正在尝试使用 selenium 抓取网页。通过检查页面和右键单击建议的 xpath 属于不稳定类型 (/html/body/table[2]/tbody/tr[1]/td/form/table/tbody/tr[2]) 。所以我尝试了以下解决方案:

driver = webdriver.Chrome("path")
driver.get("https://www.bundesfinanzhof.de/entscheidungen/entscheidungen-online")
time.sleep(1)
links=driver.find_element_by_xpath('//tr[@class="SuchForm"]')

甚至

links=driver.find_elements_by_xpath('//*[@class="SuchForm"]')

不返回任何结果。但是在页面的前面,我可以获得:

links=driver.find_element_by_xpath('//iframe')
links.get_attribute('src')

之后好像:

<script language="JavaScript" src="/rechtsprechung/jscript/list.js" type="text/javascript"></script>

我无法再接触任何元素。 How do I determine the correct XPath? 表明脚本中的部分无法解析。然而,在我看来,我所追求的道路并不在一条道路上。我是否误解了脚本在页面上的工作方式?

比如后面有一条路径:

/html/body/table[2]/tbody/tr[1]/td/script

我希望这会造成这样的问题。我绝不是程序员,所以我对这个主题的理解有限。有人能解释一下问题是什么,如果可能的话有解决办法吗?

尝试使用以下解决方案:

Find element text using xpath in selenium-python NOt Working

xpath does not work with this site, pls verify

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    table 位于iframe 内,因此您需要在处理所需的tr 之前切换到该iframe

    from selenium.webdriver.support.ui import WebDriverWait as wait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver.get("https://www.bundesfinanzhof.de/entscheidungen/entscheidungen-online")
    wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://juris.bundesfinanzhof.de/cgi-bin/rechtsprechung/list.py?Gericht=bfh&Art=en']")))
    link = driver.find_element_by_xpath('//tr[@class="SuchForm"]')
    

    使用driver.switch_to.default_content()iframe切换回来

    【讨论】:

      猜你喜欢
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2012-06-27
      • 1970-01-01
      相关资源
      最近更新 更多