简体   繁体   中英

Select child of List WebElement - Java - Selenium WebDriver

I have a table that I need to say, if row contains text then click cells on that row.

I am trying to create a list of the rows, iterate through the columns of each row and if the text is found in the row, click the cell to deselect the item like the following (which obviously doesn't work, just trying to figure out the logic).

// Deselect Pending Quality Review
List<WebElement> parents = driver.findElements(By.cssSelector("tr[type='row']"));
for(WebElement parent : parents) 
{
    List<WebElement> children = parent.findElements(By.cssSelector("td[class='grdCell']"));
    for(WebElement child : children)
    {
        if (parent.getText().contains("Pending Quality Review") && child.getAttribute("style").contains("visible"))
        {
            System.out.println("Deselect Pending Quality Review");
            child.click();
        } //end if
    } // end child for loop
} // end for parent element

Here is some example HTML of what I'm working with. The first row has 2 selection boxes while the second row only has 1 as indicated by the img tag.

<tr type="row" adr="14" tag="" id="x:1011327536.13:adr:14:tag:">
    <td type="cell" adr="0" idx="0" class="grdCell">Cancelled</td>
    <td class="ig9a63765d">1</td>
    <td class="ig9a63765e">1</td>
    <td type="cell" adr="3" idx="3" class="grdCell">
        <img id="ctl00_ctl00_ctl00_Main_Main_Main_gridStatusSyncMappings_it3_14_imgSync" src="Images/BlueArrowLeft32x32.png" alt="" style="visibility:visible;"></td>
    <td class="ig9a63765f">1</td>
    <td class="ig9a637660">1</td>
    <td class="grdCell">
        <img id="ctl00_ctl00_ctl00_Main_Main_Main_gridStatusSyncMappings_it6_14_imgSync" src="Images/BlueArrowRight32x32.png" alt="" style="visibility:visible;"></td>
    <td type="cell" adr="7" idx="7" class="grdCell">Cancelled</td>
    <td class="ig9a637661">300020</td>
    <td class="ig9a637662">1</td>
    <td class="ig9a637663">1</td>
    <td class="ig9a637664">1</td>
    <td class="ig9a637665"></td>
    <td class="ig9a637666">1</td>
    <td class="ig9a637667"></td>
</tr>

<tr type="row" adr="15" tag="" id="x:1011327536.13:adr:15:tag:" class="ig_ListAlt igg_ListAlt ">
    <td type="cell" adr="0" idx="0" class="grdCell">Pending Quality Review</td>
    <td class="ig9a63765d">1</td>
    <td wlkd="1" type="cell" idx="2" adr="2" class="ig9a63765e">1</td>
    <td wlkd="1" type="cell" adr="3" idx="3" class="grdCell">
        <img id="ctl00_ctl00_ctl00_Main_Main_Main_gridStatusSyncMappings_it3_15_imgSync" src="Images/BlueArrowLeft32x32.png" alt="" style="visibility: visible;">   
    </td>
    <td class="ig9a63765f">0</td>
    <td class="ig9a637660">0</td>
    <td type="cell" adr="6" idx="6" class="grdCell grdCell_CursorDefault grdCell_Gray grdCell"></td>
    <td type="cell" adr="7" idx="7" class="grdCell">Pending Quality Review</td>
    <td class="ig9a637661">301002</td>
    <td wlkd="1" type="cell" idx="9" adr="9" class="ig9a637662">1</td>
    <td class="ig9a637663">0</td>
    <td class="ig9a637664">1</td>
    <td class="ig9a637665"></td>
    <td class="ig9a637666">1</td>
    <td class="ig9a637667"></td>
</tr>

So in this example, I'm need to select the Pending Quality Review row and if the img of the cell contains visible, then click it.

In the future, I may even need to do something along the lines of saying if the row is Cancelled, only select the 2nd option. Details on how to do that would be helpful as well.

if (parent.getText().contains("Pending Quality Review") && ***child.getAttribute("style").contains("visible")***)

The child webelement is a td and does not contain the style 'visibilty: visible' thus nothing is getting clicked.

List<WebElement> children = parent.findElements(By.cssSelector("***td[class='grdCell']***"));

You have to go down to the img level to get this attribute.

Plus can you confirm the parent.getText.contains part, does it work correctly? Just for my knowledge as I am guessing you are trying to get a consolidated text in all the child tds and search for your text.

An alternate approach using locators... Copy pasted your code to come up with the following table with borders added. Does it look correct cause I based the locators on the table. (Somehow unable to attach the image, put down the link)

http://i.stack.imgur.com/mDxcP.png

For selecting rows with 'Pending Quality Review' text you can use the following locator to gather all the images which are visible --

"//td[@class='grdCell'][text()='Pending Quality Review'][1]/following-sibling::td[3]/img[contains(@style,'visible')]"

This will give you a list of images which you can click.

For selecting rows with Cancelled text and to click on the 2nd image when visible ---

"//td[@class='grdCell'][text()='Cancelled'][1]/following-sibling::td[6]/img[contains(@style,'visible')]"

One of the pitfalls is that this approach will fail if the table is restructured by shifting columns. If you add unique classes to your img tags then the numbers can be removed from the second td.

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