I have 3 links within a bento box group in our website. I need to click the specific link if the link text matches my condition (I need to click all the 3 links anyways and I need to perform different actions after clicking them). I wrote if loops within a for loop for achieving this.
When I execute this, for the first loop i.e i=0, it clicks on the correct link. For the second loop (i=1), I expect it to pick the second link. But it clicks on the third link. For the third loop as well, it clicks on the third link. Not sure what I am missing here.`
public void SeasonalLinks() throws InterruptedException
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List <WebElement> Seasonallnks = driver.findElements(By.xpath("//section[@id='seasonallinks']/ul/li/div/a"));
int sl = Seasonallnks.size();
System.out.println("Total no.of seasonal Links: "+sl);
int i=0;
for (i=0;i<sl;i++)
{
List <WebElement> Seasonallnks1 = driver.findElements(By.xpath("//section[@id='seasonal-links']/ul/li/div/a"));
String sl1 = Seasonallnks1.get(i).getText();
if (sl1.equalsIgnoreCase("Start a provider search"))
{
Seasonallnks1.get(i).click();
WebDriverWait pshome = new WebDriverWait(driver,20);
pshome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
Thread.sleep(5000);
driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
}
else if (sl1.equalsIgnoreCase("Use pharmacy self-service tools"))
{
Seasonallnks1.get(i).click();
WebDriverWait phome = new WebDriverWait(driver,20);
phome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
Thread.sleep(5000);
WebDriverWait iswait1 = new WebDriverWait(driver,30);
iswait1.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='acc-spinner-container]")));
driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
}
else if (sl1.equalsIgnoreCase("Start rewarding yourself today"))
{
Seasonallnks1.get(i).click();
WebDriverWait hhome = new WebDriverWait(driver,20);
hhome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
}
}