【发布时间】:2018-07-27 14:20:28
【问题描述】:
我有一个类似的 html 代码:
<h3> Some Heading </h3>
<p> Some String </p>
<p> more string </p>
<h3> Other heading</h3>
<p> some text </p>
我正在尝试访问Some String、more string 和some text。使用 java,我尝试像这样访问:
List<WebElement> h3Tags = driver.findElements(By.tagName("h3"));
List<WebElement> para = null;
WebElement bagInfo = h3Tags.get(0); //reads first h3
if(bagInfo.getText().contains("carry-on") || bagInfo.getText().contains("Carry-on")){
para = AutoUtils.findElementsByTagName(bagInfo, "p");
System.out.println(para.get(0).getText()); //Null pointer here
}
bagInfo = h3Tags.get(1);
if(bagInfo.getText().contains("checked") || bagInfo.getText().contains("Checked")){
para = AutoUtils.findElementsByTagName(bagInfo, "p");
System.out.println(para.get(0).getText()); //Null pointer here too
}
尝试xpath 和"h3['/p']" 一样,但仍然没有运气。访问这些 <p> 字符串的最佳方式是什么?
【问题讨论】:
-
带 xpath //h3/following::p
标签: java selenium selenium-webdriver automation selenium-firefoxdriver