【问题标题】:Python - Selenium - webscrape xmlns tablePython - Selenium - webscrape xmlns 表
【发布时间】:2014-01-21 11:32:12
【问题描述】:
<html xmlns="hyyp://www.w3.org/1999/xhtml">
    <head>_</head>
    <body>
        <form name="Main Form" method="post" action="HTMLReport.aspx?ReportName=...">
            <div id="Whole">
                <div id="ReportHolder">
                    <table xmlns:msxsl="urn:schemeas-microsoft-com:xslt" width="100%">
                        <tbody>
                            <tr>
                                <td>_</td>
                                <td>LIVE</td>
                                and the data I need is here between <td> </td>

现在我的代码是:

import time
from selenium import webdriver

chromeOps=webdriver.ChromeOptions()
chromeOps._binary_location = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
chromeOps._arguments = ["--enable-internal-flash"]

browser = webdriver.Chrome("C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe", port=4445, chrome_options=chromeOps)
time.sleep(3)

browser.get('website')
elem=browser.find_element_by_id('MainForm')
el=elem.find_element_by_xpath('//*[@id="ReportHolder"]')

最后两行代码实际上是我测试在 xpath 崩溃之前我可以走的路径。尝试对超出此点的任何内容进行 xpath 会产生 noSuchElementException。

谁能解释一下我是如何从表格中提取数据的?

我目前的想法是,也许我必须将“某些东西”传递到 xml 树 api 并通过它访问它。虽然不知道怎么拍。

如果有人能给我下一步的工作,我将不胜感激,感觉有点像我此刻在黑暗的房间里拿着蜡烛。

【问题讨论】:

  • 澄清一下,如果您使用 xpath,例如:driver.find_element_by_xpath("//div[@id='ReportHolder']/table/tbody/tr"),您会收到异常吗?
  • 是的,这是正确的。在我的示例中: el=browser.find_element_by_xpath('//*[@id="ReportHolder"]/table/tbody/tr') 产生 noSuchElementException

标签: python xml selenium xpath


【解决方案1】:

这很简单。这是时间问题。

解决方案:在 xpath 请求之前放置一个 time.sleep(5)。

browser.get('http://www.mmgt.co.uk/HTMLReport.aspx?ReportName=Fleet%20Day%20Summary%20Report&ReportType=7&CategoryID=4923&Startdate='+strDate+'&email=false')
time.sleep(5)
ex=browser.find_element_by_xpath('//*[@id="ReportHolder"]/table/tbody/tr/td')

xpath 正在请求对动态内容的引用。

表格是动态内容,加载该内容的时间比python程序到达行的时间要长:

ex=browser.find_element_by_xpath('//*[@id="ReportHolder"]/table/tbody/tr')

从它的前一行:

browser.get('http://www.mmgt.co.uk/HTMLReport.aspx?ReportName=Fleet%20Day%20Summary%20Report&ReportType=7&CategoryID=4923&Startdate='+strDate+'&email=false')

【讨论】:

  • 与其使用time.sleep(),不如尝试使用内置的WebdriverWait 类和支持的ExpectedConditions。例如,WebdriverWait(self.druver, 5).until(ExpectedConditions.presence_of_element_located((By.XPATH, "xpath here"))
猜你喜欢
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多