How do I use Click Element function with robot framework when the element does not have id or name?

青春壹個敷衍的年華 提交于 2021-02-18 12:46:09

问题


I'm currently using the Selenium2Library in robot framework to automate some web tests. Currently, I'm having problems automating the click of a login button with the Click Element function.

This is the element I would like to use:

<a class="transparentBtn loginLink ng-scope" ng-click="commonService.gigyaRaasLogin()" translate="BTN_ADMIN_LOGIN_WATCHLIST">LOGIN</a>

and this is the xpath if I copy from the console: //*[@id="menu"]/div/div[5]/div/div/div[2]/ul/li[2]/a

I have trouble finding which locator to use if I want to click this element.

These are some of the things I've tried so far:

  1. Click Element css=a.loginLink
  2. Click Element link=LOGIN

回答1:


Try to locate unique css (1 matching node) or try this

Wait Until Element Is Visible   xpath=//*[@id="menu"]/div/div[5]/div/div/div[2]/ul/li[2]/a      10
Click Element    xpath=//*[@id="menu"]/div/div[5]/div/div/div[2]/ul/li[2]/a



回答2:


I recommend to be a bit more flexible. The good approach if you find the balance between define flexible and unique. Otherwise the smallest site change will breake your test.

Following example should match on the previous example:

  • Match on any link that contain LOGIN text

    Click Element       //a[contains(text(),'LOGIN')]
    
  • Match on any element that contain LOGIN text

    Click Element       //*[contains(text(),'LOGIN')]
    
  • Match on any element where the class attribute equal with "transparentBtn loginLink ng-scope"

    Click Element       //a[@class="transparentBtn loginLink ng-scope"]
    
  • You can check multiple attributes at the same time

    Click Element       //a[@class='transparentBtn loginLink ng-scope' and @ng-click='commonService.gigyaRaasLogin()']
    
  • You can use contains() to check if string part of the class attribute

    Click Element       //a[contains(@class,'loginLink')]
    



回答3:


To answer the question in the title, the Selenium2Library supports many different locators. The most expressive is xpath, which can be used to find just about anything. See the section Locating or specifying elements in the Selenium2Library documentation.

In your case, if you are clicking on a link then click link link=LOGIN should work. If it doesn't, it could be that the link is in an iframe, or perhaps it's hidden by css (some frameworks like angular and react will hide elements and replace them with their own thing)



来源:https://stackoverflow.com/questions/45324287/how-do-i-use-click-element-function-with-robot-framework-when-the-element-does-n

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