简体   繁体   English

使用Css表中的硒定位单元

[英]Selenium Locating Cell in Table Using Css

Selenium cannot seem to locate an input element in a table that I specified using the following code: Selenium似乎无法在我使用以下代码指定的表中找到输入元素:

driver.findElement(By.cssSelector("td:contains('Community Member')+td+td>input:contains('ACTION_DELETE')")).click();

It's throwing an InvalidSelectorException. 它抛出一个InvalidSelectorException。 The following is a snippet of the row in the page source I'm working with: 以下是我正在使用的页面源中的行的摘要:

<tr id="aui_3_2_0_1240" class="portlet-section-alternate results-row alt lfr-role lfr-role-community last">
    <td class="align-left col-1 first valign-middle" headers="issq_col-1" colspan="1"> Community Member </td>
    <td class="align-left col-2 valign-middle" headers="issq_col-2" colspan="1">
    <td id="aui_3_2_0_1239" class="align-left col-3 valign-middle" headers="issq_col-3"     colspan="1">
        <input id="aui_3_2_0_1211" type="checkbox" name="10124_ACTION_DELETE"/>
    </td>
    <td class="align-left col-4 valign-middle" headers="issq_col-4" colspan="1">
    <td class="align-left col-5 valign-middle" headers="issq_col-5" colspan="1">
    <td class="align-left col-6 valign-middle" headers="issq_col-6" colspan="1">
    <td class="align-left col-7 valign-middle" headers="issq_col-7" colspan="1">
    <td class="align-left col-8 valign-middle" headers="issq_col-8" colspan="1">
    <td class="align-left col-9 last valign-middle" headers="issq_col-9" colspan="1">
</tr>

And I am trying to intelligently click the checkbox denoted by the input element above. 我正在尝试智能地单击上面输入元素表示的复选框。 I should note that all the td's above are also expandable to reveal an input element. 我应该注意,上面所有的td都可以扩展以显示输入元素。 Ideally, what I would like is just some code that I can give the value of the first cell in the row as well as part of the name of the checkbox and have it click the checkbox for me. 理想情况下,我想要的只是一些代码,我可以给出行中第一个单元格的值以及复选框名称的一部分,并让它单击我的复选框。 In the example above that would be like ... 在上面的示例中,这就像...

clickCheckboxInTable("Community Member","ACTION_DELETE");

Any suggestions as to how to best accomplish this or fix my existing code? 关于如何最好地做到这一点或修复现有代码的任何建议?

You're attempting to use the :contains pseudoselector in your CSS selector. 您正在尝试在CSS选择器中使用:contains伪选择器。 This pseudoselector is not a part of any published CSS standard. 该伪选择器不属于任何已发布的CSS标准。 Unfortunately, it's become common since the JavaScript CSS selector engine implementation used by jQuery ( Sizzle ) allows it to be used. 不幸的是,由于jQuery( Sizzle )使用的JavaScript CSS选择器引擎实现允许使用它,因此它变得很普遍。 Since the driver implementations rely on the browsers' native CSS selector engine implementations, they don't support the :contains pseudoselector, and throw the exception you're seeing. 由于驱动程序实现依赖于浏览器的本机CSS选择器引擎实现,因此它们不支持:contains伪选择器,并引发您所看到的异常。 Also, I note that you're attempting to find the check box where the name attribute (not the element content) contains the string for which you're searching. 另外,我注意到您尝试查找复选框,其中name属性(不是元素内容)包含要搜索的字符串。 In that case :contains wouldn't work anyway. 在这种情况下, :contains不能正常工作。

Leaving aside the inherent instability of finding elements using the text contained within them, if you really want to do that, finding by XPath is likely your only option. 抛开使用元素中包含的文本查找元素所固有的不稳定性,如果您确实想这样做,则XPath查找可能是您唯一的选择。 Also, the check box element is not a descendent of the element containing the text you're looking for. 另外,复选框元素不是包含您要查找的文本的元素的后代。 Rather, it's a sibling element, and CSS selectors don't permit you to walk up the DOM tree to parent elements. 相反,它是一个同级元素,并且CSS选择器不允许您将DOM树移至父元素。 Again, this is a feature best handled by XPath. 同样,这是XPath最好处理的功能。

I'd handle the issue using something like the following: 我将使用以下方法处理该问题:

public void clickCheckboxInTable(String value, String action) {
  // Assume the WebDriver instance is stored in a class-level
  // variable named 'driver'.
  WebElement tableCell = driver.findElement(By.xpath("//td[contains(., '" + value + "')]"));
  WebElement checkbox = tableCell.findElement(By.xpath("..//input[contains(@name, '" + action + "')]"));
  checkbox.click();
}

Note carefully that this solution performs two finds. 请注意,此解决方案执行两个发现。 Doing so in two steps gives you the opportunity to perform other actions related to the cell containing your value if needed. 分两步进行操作使您有机会根据需要执行与包含您的值的单元格相关的其他操作。 Otherwise, you could use a single XPath expression like this: 否则,您可以使用单个XPath表达式,如下所示:

// Using the 'direct parent' operator ('..'), but you could also
// use other axis functions like 'following-sibling'.
findElement(By.xpath("//td[contains(., '" + value + "')]/..//input[contains(@name, '" + action + "')]"));

Try with this Xpath: 尝试使用此Xpath:

//*[@type='checkbox'][contains(@name,'_ACTION_DELETE')]

Edit 编辑

//*[normalize-space(text())='Community Member']/input[@type='checkbox'][contains(@name,'_ACTION_DELETE')]

在此处输入图片说明

EDIT: Corrected after Jim Evans comment 编辑:吉姆·埃文斯评论后更正

//*[normalize-space(text())='Community Member']/..//input[@type='checkbox'][contains(@name,'_ACTION_DELETE')]

尝试使用此xpath单击与“社区成员”相关的复选框

driver.findElement(By.xpath("//td[contains(text(),'Community Member')]/following-sibling::td/input[contains(@name,'ACTION_DELETE')]")).click();

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

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