Adam
Adam

Reputation: 370

How to get an element on a page, click something else and wait for the original element to dissapear

I am using Python 2.7, Robot Framework and Selenium2Library on a windows server.

On a page with a button and a data text box, I have a selenium test that clicks the button, this fires off a get request and on return recreates the data text box and updates with the new value. The test then sleeps for 10s to allow this to happen and then reads the value in the data text box.

What I would really like to do is get the element for the data text box, then click the button and keep polling for the original data text reference to become unavailable on the DOM i.e. it has been recreated, and then read the text box to get the value.

I can't for the life of me find out how I would do that using Robot Framework and Selenium2Library as all the calls are self contained and don't pass references back.

Could you offer any other solution?

Upvotes: 1

Views: 1198

Answers (1)

Todor Minakov
Todor Minakov

Reputation: 20067

There are a number of ways to do it with the SeleniumLibrary, all revolving around its Wait Until keywords - documentation link.

Option one - the element is the same, so its locator doesn't change; the check is done with Wait Until Element Contains, and passing the new text:

Click Element    ${the_locator_for_the_button}
Wait Until Element Contains    ${the_locator_for_the_element}    your target text

Option two - if on click the target element changes to a different one, e.g. the locator is different. Then you'd first wait for the initial element to disappear, and the new one to appear:

Click Element    ${the_locator_for_the_button}
Wait Until Element Is Not Visible    ${the_locator_for_the_initial_element}
Wait Until Element Is Visible    ${the_locator_for_the_new_element}

Option three - if you don't want to deal with locators, but to make sure the actual element as Selenium sees it disappears, you can get it with Get Webelement, and then pass that reference to the Wait ... keywords - most (if not all) SeleniumLibrary support both locators or acutal webelements:

Click Element    ${the_locator_for_the_button}
${webelement}=    Get Webelement    ${the_locator_for_the_initial_element}
Wait Until Element Is Not Visible    ${webelement}

The good thing about the Wait Until ... keywords is that they constantly poll the DOM for the expected change, and continue at the first detection the condition is met. E.g. it's not a hardcoded sleep that'll pause the execution for the predefined time, but finish as soon as ready.

Have also in mind all these keywords support the argument timeout=Xs, where the X is the time up to which the keyword waits for the condition to be met.

Upvotes: 5

Related Questions