【问题标题】:I cannot click on this button using Selenium no matter what Locator I use无论我使用什么定位器,我都无法使用 Selenium 单击此按钮
【发布时间】:2019-03-09 01:37:57
【问题描述】:

无论我使用什么定位器,我都无法单击此按钮,而且我无法理解原因。我使用过 linkText、partialLinkText、x-path、className 等。我在页面对象模型设计模式中使用 PageFactory。

这是按钮所在的 div 的代码:

<div class="clearfix main-page-indent">
<a href="http://automationpractice.com/index.php?controller=address" title="Add an address" class="btn btn-default button button-medium">.   <span>Add a new address<i class="icon-chevron-right right"></i></span></a>
</div>

这里是源代码的链接: 查看源代码:http://automationpractice.com/index.php?controller=addresses

您只需先创建一个帐户,它超级快。

以下是我尝试过的两种方法:

1.)

//Page Factory
@FindBy(how = How.PARTIAL_LINK_TEXT, using = "Add an address")
WebElement newAddressBtn;

public AddNewAddressPage clickAddNewAddressBtn() {
    newAddressBtn.click();
    return new AddNewAddressPage();
}

2.)

@FindBy(xpath = "//a[@href='http://automationpractice.com/index.php?controller=address']")
WebElement newAddressBtn;

有人知道如何整合这个定位器吗?

感谢您的帮助!

【问题讨论】:

  • 马上,当根据您的 HTML 的实际链接文本是“添加新地址”时,它不会找到“添加地址”的部分链接文本,但即便如此,该文本在 span 标签内。
  • 这个 HTML sn-p 是否在 IFRAME 中?

标签: selenium xpath testng href findby


【解决方案1】:

你能试试下面的 xpath 来定位你的元素吗?

    @FindBy(xpath = ".//*[@id='center_column']/div[2]/a")
    public WebElement addressNew;

【讨论】:

    【解决方案2】:

    感谢您的建议,但我发现了我的错误。并不是无法解析定位器,而是因为我未能初始化 PageFactory 变量而导致出现空指针异常。我添加了这个构造函数来解决这个问题。这样,当页面类被实例化时,所有的 WebElements 都会被初始化。

    public MyAddressesPage() {
        PageFactory.initElements(driver, this);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 2021-02-27
      • 1970-01-01
      相关资源
      最近更新 更多