2017-09-26 54 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));