【问题标题】:Clicking span button by id of the parent(with id) - selenium with java按父项的 id(带 id)单击 span 按钮 - selenium with java
【发布时间】:2016-08-22 19:36:26
【问题描述】:

是否可以单击div 元素下的span 按钮,我也想使用输入的 id 来使我的测试稳定。

HTML:

<div class="idit-go">
<input id="IDITForm@checkRuleVO" class="GoLong align-left idit-go-text" type="text" value="" title="Validation Rule" tabindex="257" name="unknown" ignoredisableenableaction="true" helptextinfo="" disabled="">
<div class="idit-go-buttons">
<span id="IDITForm@checkRuleVOGoClearButtonClear" class="idit-go-clear" title="Clear" tabindex="259" onclick="clearField('IDITForm@checkRuleVOhidden');clearField('IDITForm@checkRuleVO');;">
<span class="fa-stack idit-go-button" value="" title="" tabindex="258" onclick="newPerformSubAction('selectRule',true);">
<i class="fa fa-stack-1x fa-square"></i>
<i class="fa fa-stack-1x fa-ellipsis-h"></i>
</span>
</div>

它在屏幕上的样子:

您可以看到右侧的 3 个点按钮,这就是我要单击的按钮。提前致谢。

【问题讨论】:

    标签: java html selenium xpath


    【解决方案1】:

    在这种情况下,您可以识别具有id 属性的span,然后只使用following-sibling(从您的示例来看,它们似乎处于同一级别)。

    driver.findElement(By.xpath(".//[@id='IDITForm@checkRuleVOGoClearButtonClear']/following-sibling::span"));
    

    【讨论】:

    • 那个id是点击“X”按钮,我想点击3点按钮(第二个跨度)
    • driver.findElement(By.xpath("//*[@id='IDITForm@requestedAmountVOGoClearButtonClear']/following-sibling::span")).click(); 为我工作,10 倍
    • 如何在不使用“X”按钮的情况下通过跟随兄弟跨度来单击 3 点按钮? (某些屏幕可能没有清除按钮)。
    【解决方案2】:

    使用 css - "span[id='IDITForm@checkRuleVOGoClearButtonClear'] + span[class='fa-stack idit-go-button']"

    【讨论】:

    • 不起作用 - 方法抛出 'org.openqa.selenium.InvalidSelectorException' 异常。
    • 你在使用 By.cssSelector() 函数吗?我给了一个 css 定位器而不是 xpath 定位器。在浏览器控制台上试一下,先检查一下。
    • 我刚刚又试了一次,现在可以了,谢谢。在其他情况下,是否有另一种方法代替 CSS?任何 xpath 相关的?
    • 对于 xpath,请参阅上面 Cosmin 发布的答案。
    • 他的回答无效,我收到 invalidSelectorException 错误
    【解决方案3】:

    好的,据我所知没有人知道这个,所以我也可以试试! :) 我们来了兹维卡:

        element = driver.findElement(By.xpath("//div[@class='idit-go']//span[@class='fa-stack idit-go-button']"));
        element.click();
    

    (定位并单击 span id="IDITForm@checkRuleVOGoClearButtonClear" 的元素)

    祝你好运!:)

    【讨论】:

    • 嘿,您的答案是单击“X”按钮,我试图单击 3 个点。 10x
    • 您好 Zvika,这与您只需更改 span/id 以找到所需按钮的技术相同。我已经编辑了我的答案,现在呢?
    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 2022-01-28
    • 2017-12-26
    • 2015-11-02
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多