問題描述
我目前正在開發一個 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.
.implicitlyWait()
實際上并不等待.它為驅動程序實例設置超時,因此您只需設置一次,而不是每次要等待時都調用它.
.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 腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!