0

当我输入错误的用户名和密码时,会弹出一个消息,并且有一个 Ok 按钮阻止我的应用程序。但我无法找到该按钮的 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]
4

1 回答 1

0

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(); 
         } 
    } 
 }
于 2013-03-26T12:29:32.653 回答