0

Unable to click on Xpath element even the correct path,tried with Webdriver wait and Javascript executor still no use.

My code:

driver.findElement(By.xpath(".//*[text()='Next']")).click();

Page:

<button id="paginationNext" class="btn pagination2__next" data-selenium="pagination-next-btn">Next</button>
  • My HTML page as below <button id="paginationNext" class="btn pagination2__next" data-selenium="pagination-next-btn">Next</button> – Sinu Reddy Feb 17 at 1:04
  • 2
    Might I ask why you use an xpath when you can easily use the id attribute? Also, it would help to know what kind of error you get when you execute it. – Daniel Feb 17 at 1:13
  • I tried with Id also same issue then only trying with Xpath, actually the above one when i clicked it should go to next page,but now it's not clicking and no error Tried the below method as well. WebElement element=driver.findElement(By.xpath(".//*[text()='Next']")); JavascriptExecutor js1 = (JavascriptExecutor) driver; js1.executeScript("arguments[0].scrollIntoView()", element); element.click(); – Sinu Reddy Feb 17 at 5:26
  • When i use id getting below error. org.openqa.selenium.WebDriverException: unknown error: Element <button id="pagination..." class="btn pagination2__next" data-selenium="pagination-next-btn">Next</button> is not clickable at point (1250, 689). Other element would receive the click: <div class="BackToTop--btn">...</div> My code : driver.findElement(By.id("paginationNext")); – Sinu Reddy Feb 17 at 6:42
  • Why xpath instead of id ? – NarendraR Feb 18 at 7:01
1

The below code is solved my problem..:)

   String pageCount = driver.findElement(By.xpath("//span[@id='paginationPageCount']")).getText();
        String[] count = pageCount.split(" ");
        int s = Integer.valueOf(count[3]);
        int k =0;
            if(k < s){
                int attempts = 0;
                 while(attempts < 2) {
                        try {
                            Actions actions = new Actions(driver); 
                            actions.click(driver.findElement(By.xpath("//button[text()='Next']"))).build().perform();
                            break;
                        } catch(StaleElementReferenceException e) {
                        }
                        attempts++;
                    }
                WebDriverWait wait = new WebDriverWait(driver, 10000);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[text()='Next']")));
            driver.findElement(By.xpath("//button[text()='Next']")).click(); 
            k++;
            }
0

Seems like there are more than one element with the id "paginationNext". Try replacing the xpath with the css locator

css=button.pagination2_next

If this also does not work, please share a part of the HTML code to arrive at a xpath/css selector that would locate the element you're looking for precisely.

  • Thanks for response,but still no luck,the below is my HTML code to arrive to the button <div class="clearfix pagination-panel" data-selenium="pagination-panel"><div id="paginationContainer" class="pagination2 clearfix" data-selenium="pagination"><span id="paginationPageCount" class="pagination2__text" data-selenium="pagination-text">Page 1 of 2</span><button id="paginationNext" class="btn pagination2__next" data-selenium="pagination-next-btn">Next</button></div></div> – Sinu Reddy Feb 17 at 14:41
  • Can you post a formatted version? – Sameem Feb 17 at 16:39
0

You have a data-selenium attribute. It would make the most sense to use that.

webDriver.findElements(By.xpath("//element[@data-selenium='pagination-next-btn']"));

  • Thanks for all responses,Problem is resolved using Actions and WebdriverWait – Sinu Reddy Feb 18 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.