【问题标题】:How to click a button in selenium with java without name or ID如何在没有名称或ID的Java中单击selenium中的按钮
【发布时间】:2019-07-15 23:53:05
【问题描述】:

如何使用 selenium 来点击这个按钮?

 <button type="submit" class="button--primary button button--icon button--icon--login"><span class="button-text">Log in</span></button>

这些我都试过了

    driver.findElement(By.className("button--primary button button--icon button--icon--login")).click();
    driver.findElement(By.xpath("//button[contains(@class='button--primary button button--icon button--icon--login')]")).click();
    driver.findElement(By.xpath("//span/button[text()='Log in' and @class='button']")).click();
    driver.findElement(By.xpath("//span/button[text()='Log in'][1]")).click();

但无济于事,求助!

【问题讨论】:

    标签: java selenium web driver


    【解决方案1】:

    看起来你上次尝试时几乎成功了。

    这应该可以工作:

    driver.findElement(By.xpath("//button//span[text()='Log in']")).click();
    

    当你使用 contains 时,@class 后面应该有一个逗号而不是等号。在这种情况下,您自己的路径会起作用:

    driver.findElement(By.xpath("//button[contains(@class,'button--primary button button--icon button--icon--login')]")).click();
    

    【讨论】:

    • 啊,非常感谢!阻止我的代码工作总是很愚蠢的事情哈哈
    • @Scrub 没问题。我经常做这些事情来发现它们:)
    • @Scrub 您应该发布另一个问题,其中包含您尝试过的代码和您尝试定位的 html 元素。 cmets 部分阻碍了解释和可见性
    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多