Selenium Webdriver - 单击后如何跳过等待页面加载并继续 [英] Selenium Webdriver - How do I skip the wait for page load after a click and continue
问题描述
我使用单击按钮的 webdriver 进行了 rspec 测试...单击按钮后,页面永远不会完全加载(这是预期且正确的行为).单击按钮后,我想等待 2 秒钟,然后导航到不同的 URL...尽管该页面尚未加载.我不想因为页面没有加载而抛出错误,我只想忽略它,然后继续,好像一切都很好.页面不应加载,这是预期的正确行为.
I have an rspec test using webdriver that clicks on a button... after clicking the button, the page never fully loads (which is expected and correct behaviour). After clicking the button, I want to wait 2 seconds and then navigate to a different URL... despite the fact that the page has not loaded. I don't want to throw an error because the page hasn't loaded, I want to just ignore that, and continue on as if everything is good. The page should never load, that is the expected and correct behaviour.
我怎样才能避免等到超时,其次,我怎样才能不抛出导致测试失败的错误.
How can I avoid waiting until the timeout, and secondly, how can I have that not throw an error which casuses the test to fail.
谢谢!
推荐答案
我遇到了类似的问题,我尝试了@Sergii 提到的这个解决方案,但出现以下错误:
I had this similar problem, I tried this solution which @Sergii mentioned but was getting below error:
org.openqa.selenium.JavascriptException: javascript error: element is not defined (Session info: chrome=84.0.4147.89)
作为一种解决方法,必须在 java 脚本代码中初始化元素.
As a workaround had to initialize element within java script code.
String script = "var element = document.querySelector('button[type=submit]');" +
"if (document.createEventObject){"+
"return element.fireEvent('onclick');"+
"}else{"+
"var evt = element.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);}" ;
感谢@Sergii 的回答,由于字符限制,我无法将其添加为评论,因此将其添加为单独的答案.
Thank you @Sergii for your answer, I was not able to add this as a comment due to limited characters, so added it as a separate answer.
这篇关于Selenium Webdriver - 单击后如何跳过等待页面加载并继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!