![](/img/trans.png)
[英]Click on element of a dynamic loaded page using Selenium WebDriver java
[英]Make Selenium Webdriver Stop Loading the page if the desired element is already loaded?
我正在創建一個測試並遇到一些問題。 這是場景。 我使用Selenium Web驅動程序在Page1上填寫表單,然后單擊按鈕提交表單。 第2頁開始加載...但問題是,Page2使用Google Analytics代碼,有時頁面停止加載需要一整年。
即使預期的元素已經存在,Selenium Web驅動程序也不會繼續,直到整個網頁完全加載。
如果預期元素已存在,如何讓Selenium繼續下一個任務或停止加載外部javascript / css?
我嘗試調整以下設置但沒有運氣。
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
臨時解決方案:滾動下面的答案!
下面給出一個鏡頭。
driver.findElement(By.tagName("body")).sendKeys("Keys.ESCAPE");
要么
((JavascriptExecutor) driver).executeScript("return window.stop");
或者,您也可以使用WebDriverBackedSelenium,如下面Vincent Bouvier的片段所示。
//When creating a new browser:
WebDriver driver = _initBrowser(); //Just returns firefox WebDriver
WebDriverBackedSelenium backedSelenuium =
new WebDriverBackedSelenium(driver,"about:blank");
//This code has to be put where a TimeOut is detected
//I use ExecutorService and Future<?> Object
void onTimeOut()
{
backedSelenuium.runScript("window.stop();");
}
資料來源: https : //sqa.stackexchange.com/a/6355
所以,我向Selenium報告了這些問題。 臨時解決方法是......搞亂Firefox的超時設置。 基本上默認情況下,Firefox會在您計時之前等待大約250秒。 您可以查看:config以獲取詳細信息。 基本上我把它調低了,所以Firefox不會等待太長時間,Selenium可以繼續,好像頁面已經完成加載:P。
其他瀏覽器可能存在類似的配置。 我仍然認為Selenium應該讓我們處理pagetimeout異常。 請務必在此處為錯誤添加星標: http : //code.google.com/p/selenium/issues/detail?id = 6867&sort = -id&cospecpec = ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner% 20總結 ,所以selenium解決了這些問題。
FirefoxBinary firefox = new FirefoxBinary(new File("/path/to/firefox.exe"));
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setAcceptUntrustedCertificates(true);
customProfile.setPreference("network.http.connection-timeout", 10);
customProfile.setPreference("network.http.connection-retry-timeout", 10);
driver = new FirefoxDriver(firefox, customProfile);
driver.manage().deleteAllCookies();
一旦檢查了元素並且您知道它存在,您可以導航到/加載不同的頁面(如果下一個任務在不同的頁面上)或者任務是在同一頁面上(就像你做的那樣)不需要尚未加載的元素),你可以像往常一樣繼續 - selenium將識別已經加載的元素。 當我使用功能豐富的頁面時,這對我有用。
而不是使用webdriver click()提交表單使用jsexecutor並單擊。 Jsexecutor不會等待頁面加載,您可以使用其他操作。
根據上面解釋的場景,我覺得最好在第一頁中使用下面的wait命令。
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>)));
在第一頁中找到所需元素后,您可以繼續第二頁。
根據上面解釋的場景,我覺得最好在第一頁中使用下面的wait命令。
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element =
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>)));
在第一頁中找到所需元素后,您可以繼續第二頁。
使用explicit / webdriver等----
WebDriverWait wt=new WebDriverWait(driver, 20);
wt.until(ExpectedConditions.elementToBeClickable(By.name("abc")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.