2017-09-26 42 views
4

我有一個web應用程序,我按下提交按鈕,直到數據可用的表。當我沒有數據可用,然後提交按鈕隱藏。因此,我們可以得到邏輯,直到提交按鈕隱藏,我們將click.and當按鈕不可用時,我們會顯示成功消息並加載下一個瀏覽器的網址。單擊webelement直到隱藏

for (k=0;k>30;k++) { 
    try { 
     driver.findElement(By.xpath(".//*[@id='content']/input")).click(); 
     driver.switchTo().alert(); 
     driver.switchTo().alert().accept(); 
     Thread.sleep(20000); 
    } catch (org.openqa.selenium.NoSuchElementException e){ 
     System.out.println(""+location+" Done"); 
    } 
} 

這裏driver.findElement(By.xpath(".//*[@id='content']/input")).click();此行點擊我提交button.And後提交一個瀏覽器的警報顯示這就是爲什麼我接受這一點。在這個循環for (k=0;k>30;k++)盲目我把30..Is有任何邏輯或如何管理這個任何sugggestion ... enter image description here

+0

如何使用do-while循環以刪除「30」計數器的需要?在while循環中,使用ExpectedCondition.invisibilityOf(btn)(!ExpectedCondition.invisibilityOf)的虛假性作爲布爾標誌,以在按鈕不可見時退出循環。刪除thread.sleep並在預期的條件中使用等待時間。 – Grasshopper

回答

0

您可以使用該元素的存在,在方式是這樣的一個: -

By by = By.xpath(".//*[@id='content']/input"); 
List<WebElement> buttons = driver.findElement(by); 

while(buttons !=null && !buttons.isEmpty()) { 
    WebElement yourButton = buttons.get(0); // assuming only and the first element in the list 
    yourButton.click(); 
    driver.switchTo().alert(); 
    driver.switchTo().alert().accept(); // ideally this should be sufficient 
    Thread.sleep(20000); 
    buttons = driver.findElement(by); 
} 
+0

我添加了一張圖片,你可以看一看 – zsbappa

0

以下代碼可能對您有所幫助。

try { 
     while(driver.findElement(By.xpath(".//*[@id='content']/input")).isDisplayed()){ 
     driver.findElement(By.xpath(".//*[@id='content']/input")).click(); 
     driver.switchTo().alert(); 
     driver.switchTo().alert().accept(); 
     Thread.sleep(20000); 
    } 
    } 
    catch (org.openqa.selenium.NoSuchElementException e){ 
     System.out.println(""+location+" Done"); 
    } 

與findElements另一種解決方案,

 while(driver.findElements(By.xpath(".//*[@id='content']/input")).size()>0) 
     { 
     try { 
      driver.findElement(By.xpath(".//*[@id='content']/input")).click(); 
      driver.switchTo().alert(); 
      driver.switchTo().alert().accept(); 
      Thread.sleep(20000); 
     } 
     catch (org.openqa.selenium.NoSuchElementException e){ 
      System.out.println(""+location+" Done"); 
     } 
} 
+0

我添加了一張圖片,你可以看看 – zsbappa

1

只需使用isDisplayed()檢查,以檢查是否顯示元件或不

WebElement ele=driver.findElement(By.xpath(".//*[@id='content']/input")); 
//check element is present or not 
try { 
if(ele.size()>0){ 
    if(ele.isDisplayed()){ 
    ele.click(); 
    } 
    } 
    //switch to alert and perform operation 
    driver.switchTo().alert(); 
    driver.switchTo().alert().accept(); 
    Thread.sleep(20000); 
} 
catch (Exception e){ 
    System.out.println(""+location+" Done"); 
} 
+0

當沒有按鈕不可用時,它會顯示 NoSuchElementException:沒有這樣的元素:無法找到元素:{「method」:「xpath」, 「選擇器」:「.//*[@ id ='content']/input」} – zsbappa

+0

試試這個我更新我的回答 – iamsankalp89

+0

謝謝,但在這裏它只會點擊一次我想點擊按鈕直到消失。 – zsbappa

0

實現以下邏輯:

public void test() throws InterruptedException 

{ 
    WebElement button = driver.findElement(By.xpath(".//*[@id='content']/input")); 
    while(checkButton(button)) 
    { 
     button.click(); 
     driver.switchTo().alert().accept(); 
     Thread.sleep(1000); 
    } 


}    
    public static boolean checkButton(WebElement element) 
    { 
     if(element.isDisplayed()) 
     return true; 
     else 
      return false; 

    } 

一旦它可以進一步執行你的動作,它會一直點擊直到你的元素不可見。希望這會有所幫助。

+0

你試過嗎? – NarendraR

+0

其實,我從excel文件加載URL,所以我必須做一切循環內的所有內容 – zsbappa

+0

//創建一個循環來打印行中的單元格值 for(int j = 0; j zsbappa

0

下列方式解決我我Problem..Hope其將有助於未來遊客

do 
    { 
    try { 

    driver.findElement(By.xpath(".//*[@name='yt1']")).click(); 
    driver.switchTo().alert(); 
    driver.switchTo().alert().accept(); 
    Thread.sleep(20000); 
    driver.switchTo().alert(); 
    driver.switchTo().alert().accept(); 
    Thread.sleep(2000); 
    driver.navigate().refresh(); 

    } 
    catch (org.openqa.selenium.NoSuchElementException e){ 
    System.out.println(""+location+" Done"); 
    } 
    } 
    while ((driver.findElements(By.xpath(".//*[@class='google_data_save']")).size()>0)); 
相關問題