How to use watir-webdriver to wait for page load

后端 未结 7 1851
后悔当初
后悔当初 2021-02-01 21:34

Using watir-webdriver, how do I wait for a page to load after I click a link?

At the moment I am using:

sleep n

But this is not ideal a

相关标签:
7条回答
  • 2021-02-01 22:12

    I had the same problem, and I tried to fix it by combining wait_until_present and

    until browser.div(:id=>"some_div").exists? do sleep 1 end
    

    tricks povided by @marc:

    some_div = browser.div(:id => 'some_div')
    
    begin 
    
      Watir::Wait.until
        some_div.visible?
      end
    
    rescue Watir::Wait::TimeoutError
    
      until some_div.visible?
        sleep 1
      end
    end
    

    Notice that it is your own responsibility to make sure that

    div(:id => "some_div")
    

    does exist.

    0 讨论(0)
  • 2021-02-01 22:18

    The best summary is found in "Waiting".

    This is it in a nutshell:

    require 'watir-webdriver'
    b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'
    b.select_list(:id => 'entry_1').wait_until_present
    b.text_field(:id => 'entry_0').when_present.set 'your name'
    b.button(:value => 'Submit').click
    b.button(:value => 'Submit').wait_while_present
    Watir::Wait.until { b.text.include? 'Thank you' }
    
    0 讨论(0)
  • 2021-02-01 22:20

    This is how I wait for AJAX in my project:

    ajax_loader = $b.element(:xpath  => "//*[@id='spinner-modal-transparent' and @aria-hidden='true']/div/div/div/div/img[@alt='Ajax transparent loader']")
    
    if ajax_loader.exists?
      ajax_loader.wait_while_present(timeout=350)
    else
      puts "The AJAX loader was not present."
    end
    
    0 讨论(0)
  • 2021-02-01 22:20

    You can use the wait_until or waituntilExists methods.

    0 讨论(0)
  • 2021-02-01 22:23

    I don't know if they're the best way, but this is how I'm handling this for waiting for an updating div to clear:

    while browser.div(:id=>"updating_div").visible? do sleep 1 end
    

    This is how I handle waiting for something to display:

    until browser.div(:id=>"some_div").exists? do sleep 1 end
    
    0 讨论(0)
  • 2021-02-01 22:26

    Today's release adds an optional require that brings in some helpers for waiting for elements. These are not (at the moment) available in Watir 1.6, so be aware if you use both libraries side by side.

    Check "AJAX and waiting for elements" in the Watir-webdriver Wiki for more information.

    0 讨论(0)
提交回复
热议问题