我需要在硒中选择以下元素

<div class="u8-contextmenu-item u8-contextmenu-parentitem-selection" id="n59" data-helptext="Companies" data-helpuri="help://dom" style="display:block;">
    <div class="u8-contextmenu-item-image" style="width: 16px;" data- size="s">
        <span class="img u8-img-infoarea-fi-s"></span>
    </div>
    <div class="u8-contextmenu-popup-image">
        <span class="img u8-img-arrow-r-xs"></span>
    </div>
    <div class="u8-contextmenu-item-text">Companies</div>
</div>


由于某些内部原因,我无法使用id,因此我需要通过data-helptext元素进行访问,但我不知道如何操作。

data-helptext="Companies"


您能否给我一些提示,如何解决呢?

最佳答案

使用以下代码来获取具有属性data-helptext的webelement:

driver.findElement(By.xpath("//div[@data-helptext='Companies']");


如果您有多个具有相同属性和值的HTML元素。采用

List<WebElement> expectedElelemnts = driver.findElements(By.xpath("//div[@data-helptext='Companies']");


现在遍历Web元素列表,并检查期望的元素。

07-28 01:58
查看更多