How to set the checkbox in the first column based on the value in the second column of a table?

StackOverflow https://stackoverflow.com/questions/15786300

Вопрос

I'm automating a task using Java and Selenium.

I want to set a checkbox (which is in the first column of a table) based on whether the value in the second column matches my input value. For example, in the following code snippet, the value "Magnus" matches my input value so I want to set the checkbox associated with it.

<table class="cuesTableBg" width="100%" cellspacing="0" border="0" summary="Find List Table Result">
    <tbody>
        <tr class="cuesTableBg">
        <tr class="cuesTableRowEven">
        <tr class="cuesTableRowOdd">
            <td align="center">
                <input class="content-nogroove" type="checkbox" name="result[1].chked" value="true">
                <input type="hidden" value="1c62dd7a-097a-d318-df13-75de31f54cb9" name="result[1].col[0].stringVal">
                <input type="hidden" value="Magnus" name="result[1].col[1].stringVal">
            </td>
            <td align="left">
                <a class="cuesTextLink" href="userEdit.do?key=1c62dd7a-097a-d318-df13-75de31f54cb9">Magnus</a>
            </td>
            <td align="left"></td>
            <td align="left">Carlsen</td>
            <td align="left"></td>
        </tr>
        <tr class="cuesTableRowEven">
    </tbody>
</table>

But I'm unable to do it. In the above case, the following two lines serve the purpose (as my input value matches with that in the second row):

WebElement checkbox = driver.findElement(By.xpath("//input[@type = 'checkbox' and @name = 'result[1].chked']"));
checkbox.click();

But it can't be used as the required value might not always be in the second row.

I tried following code block but to no avail:

List<WebElement> rows = driver.findElements(By.xpath("//table[@summary = 'Find List Table Result']//tr"));
for (WebElement row : rows) {
    WebElement userID = row.findElement(By.xpath(".//td[1]"));
    if(userID.getText() == "Magnus") {
        WebElement checkbox = row.findElement(By.xpath(".//input[@type = 'checkbox']"));
        checkbox.click();
        break;
    }
}

For what it's worth, XPath of the text in the second column:

/html/body[@id='mainbody']/table/tbody/tr/td/div[@id='contentautoscroll']/form/table[2]/tbody/tr[3]/td[2]/a

I don't know about CSS Selectors. Will it help here?

Это было полезно?

Решение

If you know the input value already you just use below xpath to select respected checkbox

"//a[text(),'Magnus']/parent::td/preceding-sibling::td/input[@type='checkbox']"

Update:

"//a[text()='Magnus']/parent::td/preceding-sibling::td/input[@type='checkbox']"

Другие советы

While comparing two strings equals should be used instead of ==

Replace

 if(userID.getText() == "Magnus")

with

String check1 = userID.getText();
if(check1.equals("Magnus")

Seems like it was a silly mistake on my part. The following code snippet, a fairly straightforward one, worked.

List<WebElement> rows = driver.findElements(By.xpath("//table[@class='cuesTableBg']//tr"));

for (WebElement row : rows) {
    WebElement secondColumn = row.findElement(By.xpath(".//td[2]"));
    if(secondColumn.getText().equals("Magnus")) {
        WebElement checkbox = row.findElement(By.xpath(".//td[1]/input"));
        checkbox.click();
        break;      
    }   
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top