Selenium 在进入下一页之前需要睡眠 [英] Selenium needs a sleep before going to the next page

查看:38
本文介绍了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 在进入下一页之前需要睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆