gpt4 book ai didi

java - 尝试时没有此类元素异常

转载 作者:行者123 更新时间:2023-12-01 18:16:19 24 4
gpt4 key购买 nike

为什么当代码正确时,我会收到 no such element 异常错误,当我单击 gmail 时,链接会在新选项卡中打开,但不会将电子邮件作为 key 发送?

@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();

}

最佳答案

如果它打开一个新选项卡,您需要先切换到该选项卡窗口,然后再执行该选项卡窗口的操作。请尝试下面

  // 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();

关于java - 尝试时没有此类元素异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60358048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com