Selenium @FindBy linkText or @FindBy partialLinkText not working

陌路散爱 提交于 2019-12-13 09:29:19

问题


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;

I also tried with:

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

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.


回答1:


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;
    



回答2:


Change Transfer to My transfer

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

OR Change Transfer to transfer

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



回答3:


I hope this will work

@FindBy(xpath = "//a[text()='My transfer']");
WebElement Transferbtn;


来源:https://stackoverflow.com/questions/51893393/selenium-findby-linktext-or-findby-partiallinktext-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!