当代码正确时,为什么我没有出现此类元素异常错误,当我单击gmail时,链接在新标签页中打开,但没有将电子邮件作为密钥发送?
@Test
public static void gDem() throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "D:\\Edu Stuff\\Selenium\\Selenium Softs\\Other Softs\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("http://www.google.ca");
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys("abhi.abhib5");
Thread.sleep(2000);
driver.close();
}
答案 0 :(得分:0)
如果打开新标签页,则需要先切换到该标签页窗口,然后再执行该标签页窗口的操作。请尝试以下
// this is to keep your parent window
String parentWindow = driver.getWindowHandle();
driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(3000);
// since new tab is opened get new winodw handles and switch
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys("abhi.abhib5");
Thread.sleep(2000);
// if you have further actions in parent window you need to switch back to the parent window
driver.close();