简体   繁体   English

选择列表WebElement的子级-Java-Selenium WebDriver

[英]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. 这是我正在使用的一些示例HTML。 The first row has 2 selection boxes while the second row only has 1 as indicated by the img tag. 第一行具有2个选择框,而第二行仅具有1个,如img标签所示。

<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. 因此,在此示例中,我需要选择“待处理的质量检查”行,如果单元格的img包含可见的,则单击它。

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. 子Web元素是td,不包含样式“可见性:可见”,因此没有任何点击。

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

You have to go down to the img level to get this attribute. 您必须下降到img级别才能获得此属性。

Plus can you confirm the parent.getText.contains part, does it work correctly? 另外,您可以确认parent.getText.contains部分,它可以正常工作吗? 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. 就我所知,我想您正在尝试在所有子tds中获得合并的文本并搜索您的文本。

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 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. 如果将唯一的类添加到img标签,则可以从第二个td中删除数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Selenium Webdriver手动填充列表WebElement - Java Selenium webdriver filling list webelement manually Selenium webdriver 将 webelement 存储在 webelement 列表中 - Selenium webdriver store webelement in webelement list 排序WebElement列表以查找特定的元素(Selenium WebDriver / Java) - Sorting a WebElement List to find specific Element (Selenium WebDriver/Java) 使用带有Java的Selenium Webdriver滚动Webelement - Scrolling Webelement using Selenium Webdriver with Java 如何使用带有Java的Selenium WebDriver将鼠标悬停在Web元素上 - How to mouseover on a webelement using Selenium WebDriver with Java 使用 Selenium WebDriver 和 java 断言 WebElement 不存在 - Assert that a WebElement is not present using Selenium WebDriver with java 当Select和WebElement不起作用时,如何在Selenium WebDriver中打开列表并选择一个选项 - How do I open a list and select an option in Selenium WebDriver when Select and WebElement do not work Java:Selenium:WebDriver:无法解析列表 <WebElement> 宾语; 获取错误“无法转换为java.lang.String” - Java: Selenium: WebDriver: Unable to Parse a List<WebElement> Object; Get Error “cannot be cast to java.lang.String” 扩展Selenium WebDriver WebElement? - Extend Selenium WebDriver WebElement? Java Selenium - 根据值属性选择一个 WebElement - Java Selenium - Select a WebElement based on value attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM