1

I have one application in which there is one table. I have to find one element there and click on it, and if it is not there it should click on the next page, search the element and click on it. While I am automating it, before clicking only its getting scrolled to the right side and not able to identify the page

//to click on the page number in the bottom                 
List<WebElement> pages= driver.findElement(By.className("pager")).findElements(By.tagName("a"));
System.out.println(pages.size());
System.out.println(pages.get(0).getText());
System.out.println(pages.get(1).getText());

//list to store the element which I am searching                
List<WebElement> ele=driver.findElements(By.xpath("//span[text()='" + wp_id + "']"));
boolean flag=false;
for (int i=1;i<pages.size();i++){

while(!flag){

if (ele.size()>0)
{Thread.sleep(7000);
System.out.println("element is displayed");
driver.findElement(By.xpath("//span[text()='" + wp_id + "']/../..//a[text()='Edit']")).click();
flag = true;
break;
}else{

//to click on the page number                               
pages.get(i).click();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions
                                .visibilityOfElementLocated(By.xpath("//span[text()='" + wp_id + "']")));
1

You can use the following snippet:

List<WebElement> pages= driver.findElements(By.cssSelector(".pager a"));
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 10); //wait should be initialized outside the loop

for (int i = 1; i < pages.size(); i++) { //loop starts from i = 1 assuming the 0th page is already open.
    try {
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='" + wp_id + "']")))
        driver.findElement(By.xpath("Path to Edit button/hypertext")).click();

        // if required element is not found a NoSuchElementException will be thrown and we will move on to the next page,
        // else if the element is found it will be clicked and for loop will be terminated.

        break;
    } catch (NoSuchElementException e) {
        pages.get(i).click(); //move to next page
    }
}

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.