Selenium Webdriver - How to Skip Waiting for a Page to Load After a Click and Continue

I have an rspec test using webdriver that clicks a button ... after a button is clicked, the page never loads completely (which is expected and the correct behavior). After clicking the button, I want to wait 2 seconds and then go to another URL ... even though the page is not loaded. I don’t want to throw an error because the page is not loaded, I want to just ignore it and continue as if everything was fine. The page should never load, this is the expected and correct behavior.

How can I avoid waiting before the timeout expires, and secondly, how can I do this so as not to cause an error, due to which the test is interrupted.

Thank!

+3
source share
4 answers

WebDriver has a locking API and will always wait for the page to load. Instead, you can click the button using JavaScript, i.e. Run its onclick event. I am not familiar with Ruby, but in Java it will be:

WebDriver driver = ....; // Init WebDriver
WebElement button = ....; // Find your element for clicking
String script = "if (document.createEventObject){"+
      "return arguments[0].fireEvent('onclick');"+
  "}else{"+
    "var evt = arguments[0].ownerDocument.createEvent('MouseEvents');"+
    "evt.initMouseEvent('click',true,true,"+
    "element.ownerDocument.defaultView,1,0,0,0,0,false,"+
    "false,false,false,1,null);"+
  "return !element.dispatchEvent(evt);}" ;
((JavascriptExecutor)driver).executeScript(script, button);

After that you can wait 2 seconds and continue

0
source

why don't you try the simple trick of using the "wait ()" function after waitForPageToLoad (), which is why it ignores the previous command in Selenium and never fails in this step

0
source

, RSpec:

expect {
  Thread.new() {
    sleep(2)
    raise RuntimeError
  }
  theButton.click()
}.to raise_error
0

'Enter' , , webdriver ( #, Ruby):

element.SendKeys(Key, Return);

0

Source: https://habr.com/ru/post/1796179/


All Articles