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

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

问题描述

我目前正在开发一个 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() vs .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)会更好地为您服务,然后检查您希望不存在的元素的存在性,然后重新打开它.这将是 10 秒 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 Script的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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