2017-08-30 46 views
0

我的頁面包含預先搜索鏈接,其中填充搜索條件後,會出現一個加載器,它會根據搜索條件加載我的結果,然後我需要執行刪除操作。如何在執行下一步操作之前等待加載頁面?

我的腳本運行速度很快,它不會等待加載程序消失並單擊刪除,因爲它們沒有符合我的搜索條件的記錄,所以不應該發生刪除。

代碼,我used-

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

任何人誰可以幫我等待這樣的過程。

如果有任何澄清,請通知我。

+1

複製https://stackoverflow.com/questions/36590274/selenium-how-to-wait-until-page-是完全加載 –

+0

@RajuSharma我應該把它作爲重複,因爲該問題沒有標記爲正確的答案。 –

回答

0

使用顯式的等待,請把它作爲樣品例如:

WebDriverWait wait = new WebDriverWait(driver, 20); 
By element1 = By.xpath("path of element"); 

// get the button1 element 
WebElement button1= wait.until(ExpectedConditions.presenceOfElementLocated(element1)); 
+0

我應該等待哪個元素。下一個操作即,。刪除我想要執行的或當我點擊預先搜索時。 –

+0

你的操作卡在哪裏你可以使用它, – iamsankalp89

+0

我已經使用之前點擊刪除,它不工作:( –

0

使用,這是因爲樣本首先我要等About鏈接可以點擊,所以我必須使用顯式的等待,然後等待Our products鏈接可點擊。

WebDriver driver = new FirefoxDriver(); 
driver.manage().window().maximize(); 
driver.get("http://www.google.com"); 
WebDriverWait wait =new WebDriverWait(driver, 20); 
WebElement aboutLink= wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText("About")))); 

aboutLink.click(); 

WebElement producttLink= wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//a[text()='Our products']")))); 
producttLink.click(); 
+0

你能解釋我嗎,你爲什麼要對同一個問題提出多個答案..這很刺激。請僅用不同的解決方案發布答案​​。 –

+0

他想要一個相同的示例代碼 – iamsankalp89

+0

然後您可以將您的答案與正確的文本合併。不像另一個答案。 –

0

如果你知道裝載機的元素(每例如,classname="spinner「),你也可以等待該元素的隱形

試試下面的代碼:

WebDriverWait wait = new WebDriverWait(driver, 20); 
By loadElement = By.className("spinner"); 

//wait until loader is gone 
WebDriverWait.until(ExpectedConditions.invisibilityOfElementLocated(loadElement)); 
1

由於在填寫搜索條件時會出現一個加載器,它會根據搜索條件加載結果,然後您需要執行刪除操作,因此我們有兩種方法來解決此問題。

  • 在第一種方法,我們將等待加載器disappear和下一步點擊預期WebElementDelete。我們將實現通過WebDriverWaitExpectedConditions一套這種方法invisibilityOfElementLocated如下:

    WebDriverWait wait10 = new WebDriverWait(driver, 10); 
    wait10.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_loader"))); 
    driver.findElement(By.xpath("xpath_delete_button")).click(); 
    
  • 在第二種方法中,我們將等待WebElement的刪除按鈕可以點擊旁邊點擊預期WebElementDelete 。我們將通過實現與WebDriverWait一套ExpectedConditions這種方法elementToBeClickable如下:

    WebDriverWait wait11 = new WebDriverWait(driver, 10); 
    WebElement element11 = wait11.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_delete_button"))); 
    element11.click(); 
    
相關問題