java Webdriver Automation无法使用xpath找到元素(在Octane 2.0基准页面中)
我正试图用WebElements解析Octane基准页面http://octane-benchmark.googlecode.com/svn/latest/index.html:
<div class="hero-unit" id="inside-anchor">
<h1 id="main-banner" align="center">Start Octane 2.0</h1>
<div id="bar-appendix"></div>
</div>
我已经在平板电脑设备上启动了SeleniumWebDriver(使用Java、Eclipse和Selendoroid)
SelendroidConfiguration config = new SelendroidConfiguration();
selendroidServer = new SelendroidLauncher(config);
selendroidServer.lauchSelendroid();
DesiredCapabilities caps = SelendroidCapabilities.安卓();
driver = new SelendroidDriver(caps);
我已经用辛烷值页初始化了驱动程序:
driver.get("http://octane-benchmark.googlecode.com/svn/latest/index.html");
我正在尝试用xpath解析它:
String xpathString = "//div[@class='hero-unit']//h1";
String line = driver.findElement(By.xpath(xpathString)).getText();
System.out.println(line);
但是Java返回NullPointer异常(在线)-函数FindElement()无法在此上找到任何内容。html页面
驱动程序启动良好,它为getCurrentUrl()函数返回适当的值,但不能返回PageSource(),也不能为findElement(By.something…)返回任何值。 看起来,这个辛烷值页面有停止每个搜索请求的功能(在解析过程中)。以同样的方式,我已经解析了其他7个基准页面,它们工作得很好,但是这个辛烷值页面。。。行为就像它是“空”的WebDriver
我不知道是因为什么
<script type="text/javascript">
还是别的什么
这个辛烷值基准页面有什么特别之处吗
谢谢
# 1 楼答案
xPath()适用于符合XML标准的站点。HTML更宽容;您可能会丢失结束标记和其他错误,但在XML中这是禁止的。因此,html可能不符合XML标准,因此我通过验证您在该站点的链接进行了双重检查:
http://www.w3schools.com/xml/xml_validator.asp
你猜怎么着?它有一些错误。您可以通过首先在此站点上进行验证来避免下次的麻烦。当然,这并不意味着符合XML的站点都适合xPath()webscraping(隐藏元素、javascript等)。但是,从报告的错误的性质,您可能可以判断哪些错误不是
# 2 楼答案
只有当html页面符合XML标准时,
By.xpath()
才起作用。Octane2.0页面可能不符合要求,因此该方法返回null