Loop through page links in div and click each link containing a specific value in Selenium java

+1 vote

I have an html page that has a section which is structured like this:

<section id="fl-results" class="fl-results-revenue">

<li data-tab="details" class="ui-state-default ui-corner-top" role="tab">

<a class="toggle-flight-block-details ui-tabs-anchor" href="#flight-details-1" tabindex="-1" id="ui-id-3">Details</a>

</li>

<!-- Some html -->

<li data-tab="details" class="ui-state-default ui-corner-top" role="tab">

<a class="toggle-flight-block-details ui-tabs-anchor" href="#flight-details-2" tabindex="-1" id="ui-id-5">Details</a>

</li>

<!-- Some html -->

<li data-tab="details" class="ui-state-default ui-corner-top" role="tab">

<a class="toggle-flight-block-details ui-tabs-anchor" href="#flight-details-3" tabindex="-1" id="ui-id-8">Details</a>

</li>

<!-- Some html -->

<li data-tab="details" class="ui-state-default ui-corner-top" role="tab">

<a class="toggle-flight-block-details ui-tabs-anchor" href="#flight-details-4" tabindex="-1" id="ui-id-19">Details</a>

</li>

</section>

Now iIwant to click all the links for details with the section with id fl-results. 

In my code, I wait for the elements to be clickable, which is working, then try to click them all:

wait.until(ExpectedConditions.elementToBeClickable(By.className("toggle-flight-block-details")));

When I tried the code below, it only opens clicks the first instance of the link:

driver.findElement(By.className("toggle-flight-block-details")).click();

I saw a post suggesting to use cssSelector (driver.findElement(By.cssSelector("a[href*='flight-details-1']")).click();), this requires me iterating through all the links with such a class. The iteration work but the clicking gives me an error

//loop through page and click all link details

List<WebElement> allLinksWebpage = driver.findElements(By.className(waitCondition));

int k = allLinksWebpage.size();

for(int i=0;i<k;i++)

{

if(allLinksWebpage.get(i).getAttribute("href").contains("flight-details"))

{

String waitCondition = "flight-details-"+(i+1);

String link = allLinksWebpage.get(i).getAttribute("href");

driver.findElement(By.cssSelector("a[href*='"+waitCondition+"']")).click();

}

}

Error given is:

Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <a class="toggle-flight-block-details ui-tabs-anchor" href="#flight-details-2" role="presentation" tabindex="-1" id="ui-id-15">...</a> is not clickable at point (392, 730). Other element would receive the click: <section id="fl-searchcount-wrap" class="fl-searchcount-wrap sticky stuck">...</section>

I saw a post regarding a similar error and it says to use javascript instead because it has something to do with chrome, but I do not know how to implement that in my case?

Jun 13, 2020 in Selenium by laiman
• 330 points
8,848 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Selenium

0 votes
1 answer

How to Click a Link by href value in Selenium WebDriver?

Hi Ujjwal, you can use following code ...READ MORE

answered Aug 14, 2019 in Selenium by Abha
• 28,140 points
4,776 views
0 votes
1 answer

Login page test script using selenium and java in Eclipse IDE

Here is an example to login to ...READ MORE

answered Apr 23, 2018 in Selenium by Meci Matt
• 9,460 points
13,860 views
+1 vote
2 answers
+2 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP