【问题标题】:How to click on an element inside span tag? - Selenium如何单击 span 标签内的元素? - 硒
【发布时间】:2019-04-08 11:15:06
【问题描述】:

您好,我正在尝试在 Java 中的 selenium webdriver 中单击带有文本“€11”的特定按钮。

按钮位于此格式中,页面上还有另一个按钮,代码完全相同,但价格不同:

<button type="button" class="bui-button bui-spacer--medium bui-button--primary bui-button--wide">
<span class="bui-button__text">€11</span>
</button>

我该怎么做。任何帮助表示赞赏:)

Full CSS

【问题讨论】:

  • 实际上,span 是您的按钮。最简单的方法(如果我没记错的话)向 span 添加一个 id 并使用它点击
  • 使用driver.findElement(By.cssSelector("button[type='button']&gt;span[class='bui-button__text']")).click(); 告诉我
  • 你也可以使用 driver.findElement(By.xpath("//span[@class='bui-button__text']")).click();
  • 作为 ...页面上还有另一个按钮,其代码完全相同,但价格不同...您需要使用更多信息来更新问题outerHTML,所以我们可以区分这两个按钮。
  • 添加图片@DebanjanB

标签: java css selenium selenium-webdriver qwebelement


【解决方案1】:

我知道一些可以提供帮助的方法

javascript:

document.querySelector("span.bui-button__text").click()

jquery:

$("body > button > span").click()

硒(Java):

driver.findelement(By.cssSelector("button[class='bui-button bui-spacer--medium bui-button--primary bui-button--wide']").click()

希望对你有帮助!

【讨论】:

    【解决方案2】:

    您可以使用的几个选项:

    driver.findElement(By.xpath("//span[contains(text(),'€11')]")).click();
    

    点击第一个按钮:

    driver.findElement(By.xpath("(//span[@class='bui-button__text'])[1]")).click();
    

    点击第二个按钮:

    driver.findElement(By.xpath("(//span[@class='bui-button__text'])[2]")).click();
    

    注意:如果加载量有任何延迟,请使用等待

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-12
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      相关资源
      最近更新 更多