我正在尝试浏览一个表(id =" campaignAllAvailableList")并遍历一个标题列表:
(xpath = .//*[@id='campaignAllAvailableList']/li/div/div/h3
)看它是否与我想要的那个匹配。
如果是,那么我点击相应的按钮:
(xpath = .//*[@id='campaignAllAvailableList']/li/div/div[3]/a
)
<div id="campaignListWrapper" class="campaignOverviewSectionBody campaignListing" style="">
<ul id="campaignAllAvailableList" class="campaignList" style="display: block;">
<li>
<div class="timetableCard clearedGroup">
<div class="timetableCardHeader helpAnchor">
<h3 class="timetableCardTitle">November Campaign metaTitle</h3>
<img class="helpIcon" src="/images/icons/info-icon.png"/>
<div class="popUp helpPopUp">
</div>
<div class="timetableCardBody">
<div class="timetableCardActionBar clearedGroup">
</div>
</li>
<li>
<div class="timetableCard clearedGroup">
<div class="timetableCardHeader helpAnchor">
<h3 class="timetableCardTitle">qwe</h3>
<img class="helpIcon" src="/images/icons/info-icon.png"/>
<div class="popUp helpPopUp">
</div>
<div class="timetableCardBody">
<div class="timetableCardActionBar clearedGroup">
</div>
</li>
&#13;
这是我现在的java代码:
public class PromotionsPage extends MainPageTemplate
{
@FindBy(xpath=".//*[@id='campaignAllAvailableList']/li/div")
List<WebElement> campaignTable;
public PromotionsPage(WebDriver d) throws PageValidationException
{
super(d);
}
public void clickSpecificGetStartedButton(String metadataTitle)
{
for (WebElement el : campaignTable)
{
WebElement metadataTitleTextElement = el.findElement(By.xpath(".//div/h3"));
if (metadataTitle == metadataTitleTextElement.getText())
{
WebElement getStartedButton = el.findElement(By.xpath(".//div[3]/a"));
getStartedButton.click();
}
}
}
}
对于(WebElement el:campaignTable)&#34;它失败了,我认为它没有正确地将campaignTable作为列表阅读......
我试着直接找到标题列表
@FindBy(xpath=".//*[@id='campaignAllAvailableList']/li/div/div/h3")
List<WebElement> campaignTable;
并做
for (WebElement el : campaignTable)
{
System.out.println(el.getText());
}
但这也是错误的, java.lang.IllegalArgumentException:object不是声明类
的实例答案 0 :(得分:0)
单击要从循环中转义的元素后插入break;
。见下文:
public void clickSpecificGetStartedButton(String metadataTitle)
{
for (WebElement el : campaignTable)
{
WebElement metadataTitleTextElement = el.findElement(By.xpath(".//div/h3"));
if (metadataTitle == metadataTitleTextElement.getText())
{
WebElement getStartedButton = el.findElement(By.xpath(".//div[3]/a"));
getStartedButton.click();
break;
}
}
}
}
答案 1 :(得分:-1)
我修复了它,但我仍然不确定为什么原始代码不起作用
List<WebElement> campaignTable = driver.findElements(By.xpath(".//*[@id='campaignAllAvailableList']/li/div"));
for (WebElement el : campaignTable)
{
String metadataTitle = el.findElement(By.xpath(".//div/h3")).getText();
if (target.compareTo(metadataTitle)==0)
{
WebElement getStartedButton = el.findElement(By.xpath(".//div[3]/a"));
getStartedButton.click();
break;
}
}``