【问题标题】:How to click the button如何点击按钮
【发布时间】:2019-09-21 03:40:23
【问题描述】:

我已经厌倦了使用 xpathcss 定位器但都无法点击。

谁能帮忙看看为什么按钮元素与其他元素不同?

Here is the URL

代码试用:

driver.findElement(By.xpath("//span[@id='a-autoid-2']")).click();

【问题讨论】:

  • 为什么不能?你的脚本会崩溃吗?抛出错误?它是否点击了错误的东西?
  • 你想点击哪个按钮?
  • Qty:1 按钮,只是找不到元素。

标签: java selenium selenium-webdriver xpath css-selectors


【解决方案1】:

该元素不是button 元素,而是一个与DropDown 相关联的<span> 标记,由于class 属性@ 的存在而看起来像button 987654325@、a-button-dropdowna-button-small

要单击所需的元素,您可以使用以下任一Locator Strategies

  • cssSelector

    driver.findElement(By.cssSelector("span.a-button.a-button-dropdown.a-button-small span.a-dropdown-prompt")).click();
    
  • xpath

    driver.findElement(By.xpath("//span[@class='a-button a-button-dropdown a-button-small']//span[@class='a-dropdown-label']")).click();
    

【讨论】:

  • 这行得通。所以我需要定位器的组合......仍在学习并且不太确定如何获得和组合正确的定位器。谢谢!
【解决方案2】:

请改用下面的 xpath,它具有按钮角色而不是父跨度。

//span[@id='a-autoid-2']//span[@role='button']

【讨论】:

  • 线程 "main" org.openqa.selenium.NoSuchElementException 中的异常:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//span[@ id='a-autoid-2']//span[@role='button']"}
【解决方案3】:

如果您尝试修改数量(Qty), 试试下面的 CSS 选择器

driver.FindElement(By.CssSelector("#a-autoid-2 span[data-action='a-dropdown-button']")).Click();

该下拉按钮有一个属性,即 [aria-pressed="false"]。这默认为“假”,一旦单击属性更改为“真”。现在 WebDriver 找不到 Qty 下拉列表,因为上述定位器的属性已更改。单击代码后尝试使用流畅的等待来获取更新的 DOM 属性。

【讨论】:

    猜你喜欢
    • 2018-03-03
    • 2015-12-23
    • 2016-03-30
    • 2019-02-23
    • 2014-10-12
    • 2019-03-19
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多