我正在尝试使用 selenium 抓取网页。通过检查页面和右键单击建议的 xpaths 是一种不稳定的 (/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
请您参考如下方法:
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切换回来
