質問

I am trying to verify if an element is present on a page using the command if (@browser.text_field(:id => textbox).exists?), but the command is executed even before the page has loaded and it always goes to the else statement. How do i handle this?

役に立ちましたか?

解決

Watir continuing before the page has finished loading often means that the page is loading something asynchronously. In this case, you will need to tell Watir when the page is actually done loading. How you do this depends on the page, but in general, there are 2 options.

When loading content asynchronously, many applications will display a spinner or progress bar. When this element disappears, the page can be considered "loaded". Therefore, if you have one of these types of elements on the page, you can wait for it to disappear before proceeding:

@browser.div(:id => 'spinners_id').wait_while_present
if @browser.text_field(:id => textbox).exists?
    # Do stuff
else
  # Do other stuff
end

Alternatively, there maybe an element on the page that you know gets loaded last. In this case, you could wait until that element appears:

@browser.div(:id => 'some_last_element').wait_until_present
if @browser.text_field(:id => textbox).exists?
    # Do stuff
else
  # Do other stuff
end

他のヒント

@browser.text_field(:id,textbox).when_present.set "Welcome"

This will help you to reduce the unwanted wait check codes and make code more readable.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top