简体   繁体   中英

How to loop through table to find a string match in column 1, then select a dropdown on the same row in another column using Selenium WebDriver Java?

桌子

On the UI of the application page I'm testing, there is a table like the image above. The 5th column of the table, each row under the header 'Group' has a dropdown. All of the dropdowns have the same ID 'RIDs'.

Inspecting the 'Apple' warehouse element:

<div id="sdiv" …>
    <table id="table10987654321" …>
        <tbody>
            <tr …>
                <td …>
                    <a …>
                        <span …>
                            APPLE</span>
                      </a>
                </td>
…

Inspecting the Group dropdown on the same row as 'Apple' warehouse:

<div id="sdiv" …>
    <table id="table10987654321" …>
        <tbody>
            <tr …>
                <td …>
                <td …>
                <td …>
                <td …>
                <td …>
                    <span …>
                        <select id="RIDs" …>…</select>
                    </span>
…

To locate all the rows in this table, I wrote:

@FindAll(@FindBy(css="div[id='sdiv']>table[id^='table'] tr"))
List<WebElement> tableRows;

To locate the Group dropdown, I wrote:

@FindAll(@FindBy(css="div[id='sdiv']>table[id^='table'] td:nth-child(5) select[id='RIDs']"))
WebElement dropdownGroup;

After using PageFactory.initElements to initialize elements, now I want to iterate through the first column of the table, row by row, to find the warehouse that I'm looking for, in this case, 'BLUEBERRY' warehouse. If warehouse is 'BLUEBERRY', then select a group from the dropdown in the 5th column of the same row as 'BLUEBERRY' warehouse.

public void selectGroup (String string) {
for (WebElement row : tableRows) {
    String warehouseCell = row.findElement(By.cssSelector("td:nth-child(1)).getText());
    If (warehosueCell.equals("BLUEBERRY")) {
        Select group = new Select(this.dropdownGroup);
        group.selectByVisibleText(string);
        }
    Else {
        continue;
        }
    }
}

Then, in a different class, I'm calling the above method:

String string = "NEWGROUP";
GroupPage page = new GroupPage(driver, WAIT_TIMEOUT);
page.selectGroup(string);

When I run this, it selected the dropdown from the 'APPLE' warehouse row, instead of the 'BLUEBERRY' warehouse row.

How can I change the code so that it selects the dropdown on the same row as the warehouse I am looking for?

I thought about using for loop with index, but the size of the table can change, and new warehouses can be added, so I wouldn't know when to end the loop.

Finally solved it.

@FindAll(@FindBy(css="div[id='sdiv']>table[id^='table'] td:nth-child(5) select[id='RIDs']"))
List<WebElement> dropdownGroups;


public void selectGroup (String string) {
int i = 0;
for (WebElement row : tableRows) {
    String warehouseCell = row.findElement(By.cssSelector("td:nth-child(1)).getText());
    if (warehosueCell.equals("BLUEBERRY")) {
        Select group = new Select(this.dropdownGroups.get(i));
        group.selectByVisibleText(string);
        System.out.println("Group " + group.getFirstSelectedOption().getText() + " is selected.");
        }
    else {
        i++;
        continue;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM