Andy Williams
Andy Williams

Reputation: 907

Call Individual Index of WebElement List

If anyone needs reference or background here was my first question asked

Retrieving a list of WebElements and Identifying them

At this point I have retrieved a list of WebElements

@FindBy(css =   "td[id^=ctl00_SomeGridData_ucControlList_trgControlList_ctl00__]")
List<WebElement> allGridData;

At this point in my code I can use the web Element to call the index for example

allGridData.get(0).click   

however the list is not strictly integers for example if I access at the row level <tr> it would be:

ctl00_SomeGridData_ucControlList_trgControlList_ctl00__0

but if I were to call a link within row they are table data <td> broken into divs that look like this:

ctl00_SomeGridData_ucControlList_trgControlList_ctl00__ctl04_lbView
ctl00_SomeGridData_ucControlList_trgControlList_ctl00__ctl04_hlTestPlan

or this

ctl00_SomeGridData_ucControlList_trgControlList_ctl00__ctl07_lbView
ctl00_SomeGridData_ucControlList_trgControlList_ctl00__ctl07_hlTestPlan

Since all the WebElements start with a common css selector

  @FindBy(css =     "td[id^=ctl00_SomeGridData_ucControlList_trgControlList_ctl00__]")
    List<WebElement> allGridData;

how can identify a specific index that is a char value (ie ctl107) vs just an integer?

Upvotes: 3

Views: 2664

Answers (1)

David Baak
David Baak

Reputation: 934

Assuming you want two lists, one for the View Details and one for the View Test Plans, you need the $ (ends with):

@FindBy(css = "a[id$=lbView]")
List<WebElement> allDetailViewLinks;

@FindBy(css = "a[id$=hlTestPlan]")
List<WebElement> allTestPlanLinks;

But my best guess is that you want to click on a link in a specific row rather than based on index in a list of Web Elements. For instance based on the text in the td in <tr id="ctl00_SoxMain_ucControlList_trgControlList_ctl00__0" class="rgRow"> <td class="rgExpandCol" valign="top"/><td valign="top">AL-01</td>.

You need a method to get the row with specific text in the td.

WebElement getRow(String specificValue) {
    return driver.findElement(By.xpath("//td[text()='"+specificValue+"']"))
           .findElement(By.xpath(".."));
}

Then you can make the methods for detail view and test plan view.

public void openDetailsView(String specificValue) {
    getRow(specificValue)
        .findElement(By.cssSelector("a[id$=lbView]"))
        .click();
}

public void openTestPlanView(String specificValue) {
    getRow(specificValue)
        .findElement(By.cssSelector("a[id$=hlTestPlan]"))
        .click();
}

Upvotes: 2

Related Questions