Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Get All Links in Robot Framework Python

In Robot Framework, you can use the keyword "Get All Links" to count and verify all the links. In this articles, you will learn how to use the keyword "Get All Links" to get all the links that contain text. Mahara ePortfolio demo site is used as an example.

 

Code Implementation

1. Open Browser and navigate to the site "http://demo.mahara.org";

2. Count all the links with the xpath "//a";

3. Create a list item to contain each link that has text length greater than 1

4. Loop through each link and store text value in the list

5. Loop through the list and print each link

6. Close the browser

See the robot framework code below

*** Settings ***
Library           Selenium2Library
Library           Collections

*** Test Cases ***
Get All Links
    [Tags]    Links
    Open Browser    http://demo.mahara.org    firefox
    Maximize Browser Window
    Comment    Count Number Of Linkds on the Page
    ${AllLinksCount}=    Get Matching Xpath Count    //a
    Comment    Log links count
    Log    ${AllLinksCount}
    Comment    Create a list to store link texts
    @{LinkItems}    Create List
    Comment    Loop through all links and store links value that has length more than 1 character
    : FOR    ${INDEX}    IN RANGE    1    ${AllLinksCount}
    \    Log    ${INDEX}
    \    ${lintext}=    Get Text    xpath=(//a)[${INDEX}]
    \    Log    ${lintext}
    \    ${linklength}    Get Length    ${lintext}
    \    Run Keyword If    ${linklength}>1    Append To List    ${LinkItems}    ${lintext}
    ${LinkSize}=    Get Length    ${LinkItems}
    Log    ${LinkSize}
    Comment    Print all links
    : FOR    ${ELEMENT}    IN    @{LinkItems}
    \    Log    ${ELEMENT}
    Close Browser

Run the robot code and see the result below

Starting test: Robotexample.Links Test.Get All Links
20150309 12:19:20.551 :  INFO : Opening browser 'firefox' to base url 'http://demo.mahara.org'
20150309 12:19:29.102 :  INFO : ${AllLinksCount} = 15
20150309 12:19:29.106 :  INFO : 15
20150309 12:19:29.108 :  INFO : @{LinkItems} = [ ]
20150309 12:19:29.110 :  INFO : 1
20150309 12:19:29.128 :  INFO : ${lintext} = 
20150309 12:19:29.130 :  INFO : 
20150309 12:19:29.131 :  INFO : Length is 0
20150309 12:19:29.132 :  INFO : ${linklength} = 0
20150309 12:19:29.134 :  INFO : 2
20150309 12:19:29.154 :  INFO : ${lintext} = 
20150309 12:19:29.155 :  INFO : 
20150309 12:19:29.157 :  INFO : Length is 0
20150309 12:19:29.157 :  INFO : ${linklength} = 0
20150309 12:19:29.159 :  INFO : 3
20150309 12:19:29.180 :  INFO : ${lintext} = Mahara user manual
20150309 12:19:29.181 :  INFO : Mahara user manual
20150309 12:19:29.182 :  INFO : Length is 18
20150309 12:19:29.183 :  INFO : ${linklength} = 18
20150309 12:19:29.188 :  INFO : 4
20150309 12:19:29.206 :  INFO : ${lintext} = 
20150309 12:19:29.207 :  INFO : 
20150309 12:19:29.208 :  INFO : Length is 0
20150309 12:19:29.209 :  INFO : ${linklength} = 0
20150309 12:19:29.211 :  INFO : 5
20150309 12:19:29.228 :  INFO : ${lintext} = Register
20150309 12:19:29.230 :  INFO : Register
20150309 12:19:29.232 :  INFO : Length is 8
20150309 12:19:29.232 :  INFO : ${linklength} = 8
20150309 12:19:29.236 :  INFO : 6
20150309 12:19:29.253 :  INFO : ${lintext} = Lost username / password
20150309 12:19:29.255 :  INFO : Lost username / password
20150309 12:19:29.256 :  INFO : Length is 24
20150309 12:19:29.256 :  INFO : ${linklength} = 24
20150309 12:19:29.260 :  INFO : 7
20150309 12:19:29.280 :  INFO : ${lintext} = Persona
20150309 12:19:29.281 :  INFO : Persona
20150309 12:19:29.282 :  INFO : Length is 7
20150309 12:19:29.283 :  INFO : ${linklength} = 7
20150309 12:19:29.286 :  INFO : 8
20150309 12:19:29.305 :  INFO : ${lintext} = Mahara user manual
20150309 12:19:29.306 :  INFO : Mahara user manual
20150309 12:19:29.307 :  INFO : Length is 18
20150309 12:19:29.308 :  INFO : ${linklength} = 18
20150309 12:19:29.311 :  INFO : 9
20150309 12:19:29.329 :  INFO : ${lintext} = Mahara wiki
20150309 12:19:29.330 :  INFO : Mahara wiki
20150309 12:19:29.331 :  INFO : Length is 11
20150309 12:19:29.332 :  INFO : ${linklength} = 11
20150309 12:19:29.335 :  INFO : 10
20150309 12:19:29.352 :  INFO : ${lintext} = Mahara homepage
20150309 12:19:29.353 :  INFO : Mahara homepage
20150309 12:19:29.354 :  INFO : Length is 15
20150309 12:19:29.355 :  INFO : ${linklength} = 15
20150309 12:19:29.359 :  INFO : 11
20150309 12:19:29.378 :  INFO : ${lintext} = 
20150309 12:19:29.379 :  INFO : 
20150309 12:19:29.380 :  INFO : Length is 0
20150309 12:19:29.380 :  INFO : ${linklength} = 0
20150309 12:19:29.383 :  INFO : 12
20150309 12:19:29.399 :  INFO : ${lintext} = Terms and conditions
20150309 12:19:29.400 :  INFO : Terms and conditions
20150309 12:19:29.401 :  INFO : Length is 20
20150309 12:19:29.402 :  INFO : ${linklength} = 20
20150309 12:19:29.405 :  INFO : 13
20150309 12:19:29.426 :  INFO : ${lintext} = Privacy statement
20150309 12:19:29.427 :  INFO : Privacy statement
20150309 12:19:29.428 :  INFO : Length is 17
20150309 12:19:29.429 :  INFO : ${linklength} = 17
20150309 12:19:29.432 :  INFO : 14
20150309 12:19:29.447 :  INFO : ${lintext} = About
20150309 12:19:29.449 :  INFO : About
20150309 12:19:29.450 :  INFO : Length is 5
20150309 12:19:29.450 :  INFO : ${linklength} = 5
20150309 12:19:29.453 :  INFO : Length is 10
20150309 12:19:29.454 :  INFO : ${LinkSize} = 10
20150309 12:19:29.454 :  INFO : 10
20150309 12:19:29.457 :  INFO : Mahara user manual
20150309 12:19:29.458 :  INFO : Register
20150309 12:19:29.460 :  INFO : Lost username / password
20150309 12:19:29.461 :  INFO : Persona
20150309 12:19:29.463 :  INFO : Mahara user manual
20150309 12:19:29.465 :  INFO : Mahara wiki
20150309 12:19:29.467 :  INFO : Mahara homepage
20150309 12:19:29.469 :  INFO : Terms and conditions
20150309 12:19:29.471 :  INFO : Privacy statement
20150309 12:19:29.472 :  INFO : About
Ending test:   Robotexample.Links Test.Get All Links

Test result shows that the test passed