[英]Click webelement until hidden
我有一個Web應用程序,在其中按下提交按鈕直到表上的數據可用。當我沒有數據可用時,將提交按鈕隱藏起來。這樣我們就可以得到邏輯直到我們單擊提交按鈕隱藏為止。當按鈕不可用時我們顯示成功消息后,加載下一個瀏覽器網址。
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();
此行單擊我的提交按鈕。提交一個瀏覽器警報后,這就是為什么我接受這一點。 在這個for (k=0;k>30;k++)
循環中for (k=0;k>30;k++)
我盲目地拿了for (k=0;k>30;k++)
..是否有任何邏輯或建議?我該如何管理這個問題?
只需使用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");
}
您可以通過以下方式使用元素的存在:-
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);
}
下面的代碼可能會幫助您。
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");
}
}
實現以下邏輯:
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;
}
一旦您可以進一步執行操作,它將單擊直到您的元素不可見。 希望這會有所幫助。
按照我解決我的問題的方式..希望對下一個訪客有幫助
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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.