在运行脚本的页面上查找 xpath [英] Finding xpaths on pages running script
问题描述
我正在尝试使用 selenium 抓取网页.通过检查页面并右键单击建议的 xpath 属于不稳定类型 (/html/body/table[2]/tbody/tr[1]/td/form/table/tbody/tr[2]) .所以我尝试了以下解决方案:
Im trying to scrape a webpage using selenium. The xpaths suggested by inspecting the page and right clicking are of an unstable kind (/html/body/table[2]/tbody/tr[1]/td/form/table/tbody/tr[2]) . So I tried the following solution instead:
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"]')
不返回任何结果.但是在页面的早期我可以获得:
don't return any results. However earlier on in the page I can obtain:
links=driver.find_element_by_xpath('//iframe')
links.get_attribute('src')
似乎在之后:
<script language="JavaScript" src="/rechtsprechung/jscript/list.js" type="text/javascript"></script>
我无法再访问任何元素.如何确定正确的 XPath?表明脚本中的部分无法解析.然而,我所追求的道路在我看来并不在一条道路内.我是否误解了脚本在页面上的工作方式?
I can no longer get to any of the elements. How do I determine the correct XPath? suggests that parts within a script are impossible to parse. However, the path I am after seems to me not to be within a path. Am I misinterpretting how scripts work on a page ?
例如,后面有一条路径:
For instance, later on there is a path:
/html/body/table[2]/tbody/tr[1]/td/script
我希望这会造成这样的问题.我绝不是程序员,所以我对这个主题的理解是有限的.有人能解释一下问题是什么吗?如果可能的话,有解决方案吗?
I would expect this to create such a problem. I am by no means a programmer, so my understanding of this subject is limited. Can someone explain what the problem is and if possible a solution ?
尝试使用以下解决方案:
Attempted using solutions from:
在 selenium-python 中使用 xpath 查找元素文本不工作
推荐答案
table
位于 iframe
内,所以你需要切换到那个 iframe
在处理所需的 tr
之前:
The table
is located inside an iframe
, so you need to switch to that iframe
before handling required tr
:
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
这篇关于在运行脚本的页面上查找 xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!