硒需要睡眠才能进入下一页 [英] Selenium needs a sleep before going to the next page

查看:65
本文介绍了硒需要睡眠才能进入下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

// 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);
    }

很抱歉,我已经尽力弄清楚了.英语不是我的母语.感谢您的帮助.

亲切的问候.

解决方案

根据您的问题和更新的评论它引发了一个例外,即找不到网页上的元素,它是很有可能.此外,当您提到介于两者之间睡眠不是解决问题的绝妙方法时,这很正确,因为诱使Thread.sleep(1000);会降低整体测试执行性能.

现在,我在您的注释代码块中观察到的将document.readyState complete 进行比较的步骤更明智.但是有时可能会发生,尽管由于存在 JavaScript AJAX调用document.readyState作为 complete 发送给Selenium. >我们想要与之交互的元素可能不是 Visible Clickable Interactable ,这反过来可能引发相关的 Exception .

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

示例:

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

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");

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");

这篇关于硒需要睡眠才能进入下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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