关于selenium的等待时间的问题

本文探讨了如何使用WebDriver管理方法中的隐式等待、页面加载超时和脚本超时来优化自动化测试过程,避免因等待时间设置不当导致的测试失败。通过介绍FluentWait和ExpectedConditions的概念,提供了实现高效、灵活等待策略的实践指南。

 

 我们一般自动化的时候等待时间和超时可能会成为case挂掉的罪魁祸首,那么下面这两种等待设置可以解决这些问题:

Wait commands in WebDriver

Listing out the different WebDriver Wait statements that can be useful for an effective scripting and can avoid using the Thread.sleep() comamnds

After few searches and digging into the WebDriver Java doc, I managed to design a mindmap of the different WebDriver commands available

  

WebDriver.manage().timeouts()

 

implicitlyWait

 

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));




The ImplicitWait will tell the webDriver to poll the DOM for a certain duration when trying to find the element, this will be useful when certain elements on the webpage will not be available immediately and needs some time to load.
By default it ill take the value to 0, for the life of the WebDriver object instance through out the test script.

 

pageLoadTimeout//这个可以修改HTTP的等待时间哦

 

driver.manage().timeouts().pageLoadTimeout(100, SECONDS);


Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

setScriptTimeout

 

driver.manage().timeouts().setScriptTimeout(100,SECONDS);


Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.


Support.ui

 

FluentWait

 

// Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });


Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

ExpectedConditions(until这种会导致debug时产生异常等待,等我研究研究看怎么解决 嘿嘿)

 

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));


Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false.
Examples : Would include determining if a web page has loaded or that an element is visible.
Note that it is expected that ExpectedConditions are idempotent. They will be called in a loop by the WebDriverWait and any modification of the state of the application under test may have unexpected side-effects.


WebDriverWait will be used as we used in the Expected conditions code snippet as above.


sleeper is something same as the Thread.sleep() method, but this with an Abstraction around the thread.sleep() for better testability (有的时候sleep会用起来更方便,但是应该有它不优化的一面 等我研究

Selenium中,等待时间是非常重要的,它用于处理页面加载、元素定位和操作的同步问题Selenium提供了两种等待方式:隐式等待和显式等待。 1. 隐式等待(Implicit Wait):通过设置一个全局的等待时间,在查找元素时,如果元素没有立即出现,Selenium会等待一段时间再进行查找。如果在等待时间内找到了元素,则立即执行后续操作;如果超过等待时间仍未找到元素,则抛出NoSuchElementException异常。隐式等待只需要设置一次,对整个WebDriver的生命周期都有效。 示例代码: ```python from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(10) # 设置隐式等待时间为10秒 driver.get("https://www.example.com") element = driver.find_element_by_id("myElement") ``` 2. 显式等待(Explicit Wait):通过指定等待条件和最长等待时间,来等待某个特定条件满足后再执行后续操作。显式等待更加灵活,可以根据具体的条件来等待,例如等待元素可见、可点击、存在等。如果在最长等待时间内满足了条件,则立即执行后续操作;如果超过最长等待时间仍未满足条件,则抛出TimeoutException异常。 示例代码: ```python from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get("https://www.example.com") wait = WebDriverWait(driver, 10) # 设置最长等待时间为10秒 element = wait.until(EC.visibility_of_element_located((By.ID, "myElement"))) ``` 以上就是Selenium等待时间的介绍,隐式等待和显式等待都可以根据具体的需求来选择使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值