I am trying to go through a table (id="campaignAllAvailableList") and iterate through a list of titles: ( xpath = .//*[@id='campaignAllAvailableList']/li/div/div/h3
) to see if it matches with the one I want.
If it does, then I click onto the corresponding button:
(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>
This is the java code I have right now:
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();
}
}
}
}
It is failing at "for (WebElement el : campaignTable)", I think it is not reading campaignTable as a List properly...
I tried finding the list of title directly
@FindBy(xpath=".//*[@id='campaignAllAvailableList']/li/div/div/h3")
List<WebElement> campaignTable;
and do
for (WebElement el : campaignTable)
{
System.out.println(el.getText());
}
but this is giving an error too, java.lang.IllegalArgumentException: object is not an instance of declaring class
insert a break;
after clicking the element to escape from the loop. See below:
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;
}
}
}
}
I fixed it, but I am still not sure why the original code doesnt work
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;
}
}``
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.