我的方案是,当我点击URL时,页面应导航到验证窗口,输入有效的用户ID和密码后,将显示主页面,点击主页面中的任何链接后,将打开一个新窗口并显示相应的页面而不进行任何身份验证。
问题当我在代码中使用switchTo()时,单击主页面中的链接会打开一个新窗口,并再次提示我输入用户ID并传递。
**Code** import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.junit.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MKTEvent {
public WebDriver driver;
public String baseUrl;
public void testMKTEvent() throws IOException, BiffException, InterruptedException {
driver = new InternetExplorerDriver();
baseUrl = "<<URL>>";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
File f=new File("D:\\User ID.xls");
Workbook w=Workbook.getWorkbook(f);
Sheet s=w.getSheet(0);
for(int i=1;i<s.getRows();i++)
{
String uname=s.getCell(0,i).getContents();
String pass=s.getCell(1,i).getContents();
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).sendKeys(uname);
driver.findElement(By.name("j_password")).clear();
driver.findElement(By.name("j_password")).sendKeys(pass);
driver.findElement(By.id("submit")).click();
Thread.sleep(15000);
driver.findElement(By.id("Dashboard")).click();
String parentWindow = driver.getWindowHandle();
System.out.println(parentWindow);
for(String windowHandle : driver.getWindowHandles()){
if(!windowHandle.equals(parentWindow))
{
driver.switchTo().window(windowHandle);
String nt = driver.findElement(By.id("createRequest")).getText();
System.out.println(nt);
driver.findElement(By.id("createRequest")).click();
}
}
}
}
}
请帮我解决这个问题。
答案 0 :(得分:0)
使用DesiredCapabilities将解决您的问题。
DesiredCapabilities IECapabilities = DesiredCapabilities.internetExplorer();
IECapabilities.setCapability("enablePersistentHover", false);
IECapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
IECapabilities.setCapability("ignoreProtectedModeSettings", true);
IECapabilities.setCapability("ie.ensureCleanSession", false);