我的目标是要解析的HTML代码块象下面这样获得的文本,评论和回复字段作为块独立的部分:硒的webdriver findElements()对单失败行情
<div id='fooID' class='foo'>
<p>
This is the top caption of picture's description</p>
<p>
T=<img src="http://www.mysite.com/images/img23.jpg" alt="" width="64" height="108"/> </p>
<p>
And here is more text to describe the photo.</p>
<div class=comments>(3 comments)</div>
<div id='reply13' class='replies'>
<a href=javascript:getReply('13',1)>Show reply </a></div>
</div>
我的问题是Selenium的WebDriver似乎不支持HTML中的非字符串标识符(注意HTML中的类字段是'foo'而不是“foo”)。从我在Selenium文档和其他SO帖子中看到的所有示例中,后者的格式是WebDriver通常所期望的。
这里是我的各种(失败)的尝试我的Java代码中的相关部分:
java.util.List<WebElement> elementList = driver.findElements(By.xpath("//div[@class='foo']"));
java.util.List<WebElement> elementList = (List<WebElement>) ((JavascriptExecutor)driver).executeScript("return $('.foo')[0]");
java.util.List<WebElement> elementList = driver.findElements(By.xpath("//div[contains(@class, 'foo')]"));
java.util.List<WebElement> elementList = driver.findElements(By.cssSelector("div." + foo_tag)); // where foo_tag = "'foo'".replace("'", "\'");
java.util.List<WebElement> elementList = driver.findElements(By.cssSelector("'foo'"));
是否有处理这个法子?还是有其他更好的方法来提取上述字段? 其他信息:
- 我是一个HTML小白,但也在努力了解HTML代码的结构/标签
- 使用Firefox(和,因此,FirefoxDriver)
你帮助/建议非常感谢!
HTML是无效的。 – aimbire