• <bdo id='GoGbY'></bdo><ul id='GoGbY'></ul>
  1. <tfoot id='GoGbY'></tfoot>
    <i id='GoGbY'><tr id='GoGbY'><dt id='GoGbY'><q id='GoGbY'><span id='GoGbY'><b id='GoGbY'><form id='GoGbY'><ins id='GoGbY'></ins><ul id='GoGbY'></ul><sub id='GoGbY'></sub></form><legend id='GoGbY'></legend><bdo id='GoGbY'><pre id='GoGbY'><center id='GoGbY'></center></pre></bdo></b><th id='GoGbY'></th></span></q></dt></tr></i><div id='GoGbY'><tfoot id='GoGbY'></tfoot><dl id='GoGbY'><fieldset id='GoGbY'></fieldset></dl></div>

    1. <legend id='GoGbY'><style id='GoGbY'><dir id='GoGbY'><q id='GoGbY'></q></dir></style></legend>

      <small id='GoGbY'></small><noframes id='GoGbY'>

      Selenium 在进入下一页之前需要睡眠

      时间:2023-07-13
        <bdo id='1Aofy'></bdo><ul id='1Aofy'></ul>
          <tbody id='1Aofy'></tbody>

          1. <tfoot id='1Aofy'></tfoot>

          2. <i id='1Aofy'><tr id='1Aofy'><dt id='1Aofy'><q id='1Aofy'><span id='1Aofy'><b id='1Aofy'><form id='1Aofy'><ins id='1Aofy'></ins><ul id='1Aofy'></ul><sub id='1Aofy'></sub></form><legend id='1Aofy'></legend><bdo id='1Aofy'><pre id='1Aofy'><center id='1Aofy'></center></pre></bdo></b><th id='1Aofy'></th></span></q></dt></tr></i><div id='1Aofy'><tfoot id='1Aofy'></tfoot><dl id='1Aofy'><fieldset id='1Aofy'></fieldset></dl></div>

              <legend id='1Aofy'><style id='1Aofy'><dir id='1Aofy'><q id='1Aofy'></q></dir></style></legend>

              <small id='1Aofy'></small><noframes id='1Aofy'>

              1. 本文介绍了Selenium 在进入下一页之前需要睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我目前正在学习 Selenium,我学到了很多东西.社区说的一件事;是您需要尽可能避免 thread.sleep .Selenium 在替换中使用隐式和显式等待.是的,我理解这个概念.

                最近我遇到了一个问题.这就是没有特定动作的情况;从登录页面转到另一个页面,而不使用 Thread.sleep(1000).Selenium 似乎太崩溃了:它找不到某个元素.我觉得这种行为很奇怪.所以我在想这个冲突发生了,因为登录页面首先要重定向到网站的主页并且没有 Thread.sleep(1000);它想去第二页,但登录页面拒绝它,因为它想先去主页.话虽如此,这就是为什么 Selenium 会崩溃,或者你们在下面的示例中看到和奇怪的代码使用了吗?

                //当前在网页上WebElement ui_login_button = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("account-login-button")));ui_login_button.click();//点击后登录并重定向到网页线程.sleep(1000);//为什么要睡在这里?(没有这个 Selenium 崩溃)//转到第二页并执行操作等待加载(驱动程序);driver.navigate().to(URL + "/mymp/verkopen/index.html");/* ------------------------------------------------------------------公共无效waitForLoad(WebDriver驱动程序){预期条件<布尔值>pageLoadCondition = 新预期条件<布尔值>() {公共布尔应用(WebDriver 驱动程序){return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");}};//WebDriverWait wait = new WebDriverWait(driver, 30);wait.until(pageLoadCondition);}

                抱歉解释,我尽力说清楚.英语不是我的母语.感谢您的帮助.

                亲切的问候.

                解决方案

                根据您的问题和更新的评论 它引发了在网页上找不到元素的异常,它是很有可能.此外,当您提到 在两者之间设置睡眠并不是一个优雅的解决方案,这是非常正确的,因为引入 Thread.sleep(1000); 会降低整体 Test执行性能.

                现在,我在您的注释代码块中观察到将 document.readyStatecomplete 进行比较是一个更明智的步骤.但有时可能会发生这种情况,尽管由于 JavaScript<的存在,Web 浏览器会将 document.readyState 作为 complete 发送到 Selenium/em> 和 AJAX 调用 我们想要与之交互的元素可能不是 VisibleClickableInteractable这反过来可能会引发相关的异常.

                因此,解决方案是引入 ExplicitWait,即 WebDriverWait.我们将为要与之交互的元素诱导 ExplicitWait,并设置适当的 ExpectedConditions.您可以在ExplicitWait here 找到文档..p>

                一个例子:

                如果您想等待按钮可点击,预期的代码块可能与导入一起采用以下格式:

                import org.openqa.selenium.By;导入 org.openqa.selenium.support.ui.ExpectedConditions;导入 org.openqa.selenium.support.ui.WebDriverWait;//转到第二页并等待元素WebDriverWait 等待 = new WebDriverWait(驱动程序, 10);wait.until(ExpectedConditions.elementToBeClickable(By.id("id_of_the_element")));//执行动作driver.navigate().to(URL + "/mymp/verkopen/index.html");

                I am currently learning Selenium, and I learned a lot. One thing the community said; is that you need avoiding thread.sleep as much as possible. Selenium uses implicit and explicit waits in replace. Yes, I understand that concept.

                Recently I cam across a problem. This is that without a certain action; going from the login page to another page, without the use of a Thread.sleep(1000). Selenium seems too crash: that it can't find a certain element. I find this behaviour strange. So I was thinking that this conflict occurs, because of the login page that firstly wants to redirects to the main page of the website and without the Thread.sleep(1000); it wants to go to the second page but the login page refuses it because it want's to go first to the main page. With that being said is that why Selenium crashes or do you guys see and strange use of code in the example below?

                // Currently on a webpage   
                     WebElement ui_login_button = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("account-login-button")));
                            ui_login_button.click();
                //After the click it logs in and redirects to a webpage
                
                Thread.sleep(1000); // why sleep here? (without this Selenium crashes)
                
                   // Go to second page and perform actions
                
                waitForLoad(driver);
                driver.navigate().to(URL + "/mymp/verkopen/index.html");
                
                /* -------------------------------------------------------------------
                
                public void waitForLoad(WebDriver driver) {
                        ExpectedCondition<Boolean> pageLoadCondition = new
                                ExpectedCondition<Boolean>() {
                            public Boolean apply(WebDriver driver) {
                                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                            }
                        };
                
                        //WebDriverWait wait = new WebDriverWait(driver, 30);
                        wait.until(pageLoadCondition);
                    }
                

                Sorry for the explanation, I tried my best to be clear. English is not my native language. Thanks for your help.

                Kind regargds.

                解决方案

                As per your question and the updated comments It raises an exception that it can't find the element on the webpage, it is very much possible. Additionally when you mention putting a sleep in between is not an elegant solution to fix, that's pretty correct as inducing Thread.sleep(1000); degrades the overall Test Execution Performance.

                Now, what I observed in your commented code block to compare document.readyState to complete was a wiser step. But sometime it may happen that, though Web Browser will send document.readyState as complete to Selenium, due to presence of JavaScript and AJAX Calls the elements with whom we want to interact may not be Visible, Clickable or Interactable which in-turn may raise associated Exception.

                So, the solution would be inducing ExplicitWait i.e. WebDriverWait. We will induce ExplicitWait for the element with which we want to interact, with proper ExpectedConditions set. You can find documentation about ExplicitWait here.

                An Example:

                If you want to wait for a button to be clickable the expected code block may be in the following format along with the imports:

                import org.openqa.selenium.By;
                import org.openqa.selenium.support.ui.ExpectedConditions;
                import org.openqa.selenium.support.ui.WebDriverWait;        
                
                // Go to second page and wait for the element    
                WebDriverWait wait = new WebDriverWait(driver, 10);
                wait.until(ExpectedConditions.elementToBeClickable(By.id("id_of_the_element")));        
                //perform actions
                driver.navigate().to(URL + "/mymp/verkopen/index.html");
                

                这篇关于Selenium 在进入下一页之前需要睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

              2. <legend id='V5c3B'><style id='V5c3B'><dir id='V5c3B'><q id='V5c3B'></q></dir></style></legend>
                  <i id='V5c3B'><tr id='V5c3B'><dt id='V5c3B'><q id='V5c3B'><span id='V5c3B'><b id='V5c3B'><form id='V5c3B'><ins id='V5c3B'></ins><ul id='V5c3B'></ul><sub id='V5c3B'></sub></form><legend id='V5c3B'></legend><bdo id='V5c3B'><pre id='V5c3B'><center id='V5c3B'></center></pre></bdo></b><th id='V5c3B'></th></span></q></dt></tr></i><div id='V5c3B'><tfoot id='V5c3B'></tfoot><dl id='V5c3B'><fieldset id='V5c3B'></fieldset></dl></div>
                    <bdo id='V5c3B'></bdo><ul id='V5c3B'></ul>

                      <small id='V5c3B'></small><noframes id='V5c3B'>

                      <tfoot id='V5c3B'></tfoot>

                        <tbody id='V5c3B'></tbody>