Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用WebDriver处理操作系统生成的弹出窗口_Java_Webdriver_Autoit - Fatal编程技术网

Java 如何使用WebDriver处理操作系统生成的弹出窗口

Java 如何使用WebDriver处理操作系统生成的弹出窗口,java,webdriver,autoit,Java,Webdriver,Autoit,当我输入错误的用户名和密码时,一个弹出窗口显示消息,一个确定按钮阻止了我的应用程序。但我找不到该按钮的id,因为firebug没有处理该按钮。我搜索了更多的博客,我知道这是操作系统生成的弹出窗口,可以由AutoIT处理。请帮助我如何处理该弹出框。这是我的代码 ![package test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import jav

当我输入错误的用户名和密码时,一个弹出窗口显示消息,一个确定按钮阻止了我的应用程序。但我找不到该按钮的id,因为firebug没有处理该按钮。我搜索了更多的博客,我知道这是操作系统生成的弹出窗口,可以由AutoIT处理。请帮助我如何处理该弹出框。这是我的代码

![package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Read {

    public WebDriver driver;

    @BeforeMethod
    public void launch() throws Exception {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Test
    public void testImportexport1() throws BiffException, IOException,
            RowsExceededException, WriteException, InterruptedException {

        FileInputStream fis = new FileInputStream("Data//Logindev.xls");
        Workbook w = Workbook.getWorkbook(fis);
        Sheet s = w.getSheet(0);
        String a\[\]\[\] = new String\[s.getRows()\]\[s.getColumns()\];

        FileOutputStream fos = new FileOutputStream("Data//Logindev_1.xls");
        WritableWorkbook wwb = Workbook.createWorkbook(fos);
        WritableSheet ws = wwb.createSheet("LoginResult", 0);

        System.out.println("s.getRows() = " + s.getRows());

        for (int i = 0; i < s.getRows(); i++) {
            System.out.println("s.getColumns() = " + s.getColumns());

            for (int j = 0; j < s.getColumns(); j++) {
                a\[i\]\[j\] = s.getCell(j, i).getContents();
                Label l = new Label(j, i, a\[i\]\[j\]);
                Label l1 = new Label(2, 0, "Result");

                ws.addCell(l);
                ws.addCell(l1);


            }
        }
        for (int i = 1; i < s.getRows(); i++) {
            driver.get("url");

            driver.findElement(By.name("txtUserName")).sendKeys(
                    s.getCell(0, i).getContents());
            driver.findElement(By.name("txtPwd")).sendKeys(
                    s.getCell(1, i).getContents());
            driver.findElement(By.name("btnSignIn")).click();

            Thread.sleep(15000);

            // System.out.println("Title = " + driver.getTitle());
            if (driver.findElement(By.linkText("DashBoard")).isDisplayed()) {
                System.out.println("Element is found");
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.findElement(
                        By.xpath("//*\[@id='ctl00_headerContent_lnkLogOut'\]"))
                        .click();
                Thread.sleep(2000);
                Label l2 = new Label(2, i, "Pass");
                ws.addCell(l2);
            } else if (driver.getTitle().equalsIgnoreCase(
                    "Welcome to ShopMyFarm")) {

                driver.switchTo().alert().accept();
                Thread.sleep(5000);
                System.out.println("Element Not Found");
                Label l2 = new Label(2, i, "Fail");
                ws.addCell(l2);
            }
        }
        Thread.sleep(2000);
        wwb.write();
        wwb.close();
    }
}][1]
![包装测试;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.concurrent.TimeUnit;
进口jxl.Sheet;
导入jxl.工作簿;
导入jxl.read.biff.BiffException;
导入jxl.write.Label;
导入jxl.write.WritableSheet;
导入jxl.write.WritableWorkbook;
导入jxl.write.WriteException;
导入jxl.write.biff.rowseceedexception;
导入org.openqa.selenium.Alert;
导入org.openqa.selenium.By;
导入org.openqa.selenium.NoAlertPresentException;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.testng.annotations.BeforeMethod;
导入org.testng.annotations.Test;
公开课阅读{
公共网络驱动程序;
@预处理法
public void launch()引发异常{
System.setProperty(“webdriver.chrome.driver”,
“C:\\Chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe”);
驱动程序=新的ChromeDriver();
}
@试验
public void testImportexport1()引发BiffeException、IOException、,
RowsExceedException、WriteException、InterruptedException{
FileInputStream fis=新的FileInputStream(“Data//Logindev.xls”);
工作簿w=工作簿.get工作簿(fis);
表s=w.getSheet(0);
字符串a\[\]\[\]=新字符串\[s.getRows()\]\[s.getColumns()\];
FileOutputStream fos=新的FileOutputStream(“Data//Logindev_1.xls”);
WritableWorkbook wwb=工作簿.createWorkbook(fos);
WritableSheet ws=wwb.createSheet(“LoginResult”,0);
System.out.println(“s.getRows()=”+s.getRows());
对于(int i=0;i
WebDriver无法直接与对话框窗口交互这是因为对话框窗口是操作系统的域,而不是网页。我认为您可以使用java中的Robot类来执行您想要执行的键盘操作,比如在您的例子中,按Enter键关闭操作系统对话框

import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.KeyEvent; 

public class RobotExp 
{ 

  public static void main(String[] args) 
  { 

    try { 

          Robot robot = new Robot(); 
          robot.keyPress(KeyEvent.VK_ENTER); 
         } 
    catch (AWTException e) { 
           e.printStackTrace(); 
         } 
    } 
 }

我已经更新了我对您在上一个问题中共享的代码的回复,请检查这是否有用。您是指操作系统生成的本机窗口弹出窗口吗?发布生成的弹出窗口的屏幕截图。嗨,CheryJose,我尝试了你的代码。但它对我不起作用。我共享了我的代码。请给我任何建议。