how to locate an id of the button to click when id is dynamic in robot framework

1.4k views Asked by At

I am new to robot framework and learned few basics of robot framework. when i tried to create a new account in facebook , i have used id element of "create new account" but everytime new id is generated when its open an new page. can someone help to make me understand how to click a button when id is dynamic.

*** Settings ***

Library    SeleniumLibrary

*** Test Cases ***

facebooklogin

    open browser    https://www.facebook.com/    firefox
    maximize browser window
    set selenium timeout    10s
    click button    id:u_0_d_si

Error message : Button with locator 'id:u_0_d_si' not found.

3

There are 3 answers

2
Dev On BEST ANSWER

Just use proper locator strategy of the element, since you have used the id attribute of element, but if you refresh the page every time it will change, meaning id attribute value is dynamic, where as you can go for text of the element to identify it every time uniquely.

*** Test Cases ***
Create new facebook account

    Open Browser    https://www.facebook.com/    firefox
    Maximize Browser Window
    Set Selenium Timeout    10s
    Click Element    xpath:.//a[text()="Create New Account"]
1
Nek On

A fast alternative that I found is that you can open this link : https://www.facebook.com/login/web/ and then:

SeleniumLibrary.Wait Until Element Is Visible 
xpath://a[contains(text(),'Sign up for Facebook')]   10s        
Click Element    xpath://a[contains(text(),'Sign up for Facebook')]
0
tijnn On

sometimes this works great on dynamic elements:

${element1}=  Asign Id To Element  contains...etc
Execute Javascript   arguments[0].click();   Arguments   ${element1}