Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 277
Zubair Avatar asked Jul 26 '17 10:07

Zubair


People also ask

How do you click links in Robot Framework?

In the [Selenium2 Robot Framework documentation][1], the guide to Click Link is: Clicks a link identified by locator. Key attributes for links are id, name, href and link text. See introduction for details about locating elements.

How do you click a dropdown in Robot Framework?

While writing the keyword for test cases in RIDE, press Ctrl + Spacebar. This gives all the details of the command. We will work on an example to show working for all the cases mentioned above. In our test page, we will create 3 dropdowns and will use above test cases to select the dropdown by index, label and value.


1 Answers

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')]
    
like image 95
Krisz Avatar answered Sep 18 '22 17:09

Krisz