css - WebDriver Selenium:在某些元素之后需要在表中选择元素

标签 css selenium xpath selenium-webdriver

HTML页包含用户详细信息表。
我需要删除用户。
我可以通过用户名选择该行中的第一个元素。
我需要选择该行上的“删除”按钮才能删除该用户。
HTML结构是:

<table id="tblUsersGrid" cellpadding="2">  
<thead>  
<tbody>
<tr class="aboutMeRow" data-parentid="223">  
<tr>  
<tr>  
 ...
<tr>
    <td class="cell cell_278 cell_NameCell">xoxo</td>
    <td class="optionIcon overrideFloat orgIcon cell cell_278 gridCell"></td>  
    <td class="cell cell_278 gridCell">Custom</td>  
    <td class="cell cell_278 gridCell">qaadmin</td>  
    <td class="cell cell_278 gridCell">0</td>  
    <td class="cell gridCell">  
        <div class="removeAccountIcon"></div>  
    <td class="cell gridCell">  
        <div class="editAccountIcon"></div>  
    </td>  
    <td></td>  
</tr>  


所以我可以轻松地通过选择所需的行

driver.findElement(By.xpath("//td[@class='cell_NameCell'][contains(text(),'xoxo')]"))  


但是,如何到达该行的removeAccountIcon元素?
我看到了许多有关选择表中元素的问题,但没有找到解决此问题的方法。
有没有一种方法可以通过CSS选择器进行操作,而不仅仅是通过Xpath? (我确定对此有一个Xpath解决方案)。

最佳答案

Xpath轴preceding-sibling将帮助您解决问题。您可以在xpath下尝试:

//td[preceding-sibling::td[contains(text(),'xoxo')]][5]/div[@class='removeAccountIcon']

请让我知道这对你有没有用。

关于css - WebDriver Selenium:在某些元素之后需要在表中选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31372465/

相关文章:

xpath - 如何使用 Selenium/XPath 检索下面 HTML 中 TD 的类?

xml - 如何获取 xml 文件中特定值的值?

xml - 在带有 not() 的 bash 中使用 XPath

XML Xpath - 选择其中元素 = 多个值

javascript - Bootstrap 4 导航下拉悬停在桌面上/点击移动设备

javascript - 无法读取未定义/TypeError : event is undefined 的属性 'target'

java - Allure:如果子步骤失败,如何使父步骤失败?

javascript - 基础标题 Accordion 的内部链接?

html - 如何对齐下拉列表中两个单词之间的缩进空间?

selenium - 将 WebDriver promise 转换为 native promise ?