Selenium @FindBy linkText或@FindBypartialLinkText不起作用 [英] Selenium @FindBy linkText or @FindBy partialLinkText not working

查看:286
本文介绍了Selenium @FindBy linkText或@FindBypartialLinkText不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自动化工作,我试图单击一个元素,该元素没有id,类名,因此我正在使用xpath.为了改善我的代码,我想通过href找到该元素,所以我在使用:

I'm working for automation, I'm trying to click on an element ,this element has no id, classname so I'm using the xpath. To improve my code I would like to find this element by href, so I'm using:

@FindBy(linkText="Transfer")
WebElement transferBtn;

我也尝试过:

@FindBy(partialLinkText="Transfer")
WebElement transferBtn;

但是我的代码找不到webElement,它适用于其他href,但不是全部.我认为问题出在开发人员工具上,我将向您展示该元素:

But my code doesn't find the webElement, it's working for other href but not all. I think the problem is developer tools, i'll show you the element:

<a href="#">My transfer</a>

您对问题有任何想法,为什么它不起作用? 谢谢.

Do you have any idea of the problem, why it's not working? Thanks.

推荐答案

根据您共享的 HTML ,您可以使用以下任一解决方案:

As per the HTML you have shared you can use either of the following solutions:

  • linkText:

@FindBy(linkText = "My transfer")
WebElement transferBtn;

  • partialLinkText:

    @FindBy(partialLinkText = "transfer")
    WebElement transferBtn;
    

  • xpath:

    @FindBy(xpath  = "//a[contains(.,'My transfer')]")
    WebElement transferBtn;
    

  • 这篇关于Selenium @FindBy linkText或@FindBypartialLinkText不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆