如何以最短的等待时间加速 Java Selenium 脚本 [英] How to speed up Java Selenium Script,with minimum wait time

查看:30
本文介绍了如何以最短的等待时间加速 Java Selenium 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 java selenium 项目,它通常是一个小脚本,我必须在其中检查每个元素是否存在,并基于触发了一些操作,但我们主要关心的是完成脚本的持续时间.

I'm currently working on a java selenium Project, which is usually a small script where I have to check for each element for it's presence and based on that some actions are triggered but our main concern is time duration to finish the script.

基本上,我在脚本中使用了下面的每一个并运行了测试,虽然在每种情况下脚本都在运行,但我发现脚本执行持续时间的速度几乎没有提高.我正在使用等待

Basically I have used each one from below in my script and ran the test, though in each case script was running but I find very little speed improvement in script execution duration.I'm using wait

driver.manage().timeouts().implicitlyWait(10000,TimeUnit.MILLISECONDS);

和它一起

!(driver.findElement(By.xpath("Element Xpath)).isEmpty()) 

driver.findElements(By.xpath("Element Xpath)).size()>0

我知道我可以选择 CSS 选择器,但在我的情况下,由于 DOM 树结构,这是不可行的.可以用什么代替

I know I can go for CSS Selectors but in my case that is not feasible due to DOM Tree structure. what can be used instead of

driver.findElements(By.xpath("Element Xpath)).size()>0

这是检查元素是否存在,并基于此我必须触发多个其他操作.

this to check if element is present or not and based on that I have to trigger multiple other actions.

推荐答案

您的方法存在一些问题.

There are a few issues with your approach.

  1. .implicitlyWait() 实际上并不等待.它为驱动程序实例设置超时,因此您只需设置一次,而不是每次要等待时都调用它.

  1. .implicitlyWait() doesn't actually wait. It sets the timeout for the driver instance so you only need to set it once, not call it each time you want to wait.

driver.findElement(...).isEmpty() 不会编译.也许您的意思是 .findElements()?无论哪种方式, .isEmpty().size() >0 的速度差异可以忽略不计.

driver.findElement(...).isEmpty() won't compile. Maybe you meant .findElements()? Either way, .isEmpty() vs .size() > 0 is going to be a negligible difference in speed.

主要问题是在检查不存在的东西时启用了隐式等待……尤其是等待 10 秒.这意味着每次检查一个元素时,Selenium 都会等待 10 秒,即使它预计它不存在.

The main problem is that you have an implicit wait enabled when checking for something to NOT be present... especially a 10s wait. That means that each time an element is checked for, Selenium will wait for 10s even if it's expecting it to NOT be there.

关闭隐式等待(将其设置为 0)会更好地为您服务,然后检查您希望不存在的元素是否存在,然后重新打开它.那将是 10s x # 的存在检查,您希望不存在.根据您进行的存在检查次数,这可能会增加很多时间.这样做的一个缺点是,如果您有一个带有后台进程的复杂页面,您将需要等待页面(或页面的一部分)完成加载,然后再检查是否存在隐式等待关闭的元素.

You would be better served by turning off implicit wait (setting it to 0) and then do your existence check for elements you expect not to be there and then turn it back on. That will be 10s x # of existence checks you expect to not be there. Depending on how many existence checks you do, that could add up to a LOT of time. One downside of this, if you have a complex page with background processes, you will need to have a specific wait for the page (or portion of a page) to finish loading before checking for existence of elements with implicit wait off.

旁注... Selenium 贡献者声明不应使用隐式等待.改用 WebDriverWait 但这是另一回事.

Side note... Selenium contributors have stated that implicit waits shouldn't be used period. Use WebDriverWait instead but that's a whole other discussion.

这篇关于如何以最短的等待时间加速 Java Selenium 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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