familyGuy
familyGuy

Reputation: 435

How to search and enter data into the search field

My scenario:

  1. create an Id
  2. enter Id into the search field, that was created in step 1.

If the id is available in the list of ids visible/available on the current screen(screenshot below), I am ale to get the id, and enter into the search field. However, if the id is not visible/available on the current screen, the script fails to retrieve it. In that case, I will have to click on the Right-Arrow button to advance to the next list, search for the id there, and enter in the search field.

My code below gets the id, if it is available in the first screen. I did try to use if statement, but its not working. Any help is greatly appreciated!

My Script:

/*3. Enter the Site ID of the advertiser created in the previous step.*/

        //this finds the ANY <a> tag with the text supplied by the site_id
        WebElement siteId = driver.findElement(By.xpath("//a[.='" + site_id + "']"));
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[.='" + site_id + "']")));

        if(!(driver.findElement(By.xpath(PvtConstants.READ_SITES_IDS)).getText().contains("//a[.='" + site_id + "']"))){

//click on the Right-Arrow Button
            driver.findElement(By.xpath(".//*[@id='dataTableSite_wrapper']/div[3]/div[2]/span[3]")).click();

          //get a text/copy of the site_id
            String getThePreviouslyCreatedsiteId = siteId.getText().trim();

            //insert the text into the Advanced Filters Search Field
            driver.findElement(By.id(PvtConstants.READ_SITES_ADVANCED_FILTER_SITE_SEARCH_FIELD)).sendKeys(getThePreviouslyCreatedsiteId);

UI Screenshot: enter image description here

Upvotes: 0

Views: 847

Answers (1)

Vivek Singh
Vivek Singh

Reputation: 3649

In such a scenario you can do one thing...

get the no. of entries you are getting (like here 27) and divide it by no. of paginations you have to go. After that in a loop (like here it will be 3), find the element by xpath using given site_id, if its available it will break the loop else click on next page. Code will be somewhat like this:

WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement siteId = null;
while(i!=pagination){ // pagination is no. of pages you need to scrap
    try{
        siteId = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[.='" + site_id + "']"))); // will check for element to be visible, if not found then timeout exception will be thrown
        break; // if found will break the loop
    } catch(TimeoutException toe){
          driver.findElement(By.xpath(".//*[@id='dataTableSite_wrapper']/div[3]/div[2]/span[3]")).click(); // clicking next arrow 
          i++; // incrementing counter
    }
}

Upvotes: 1

Related Questions