【发布时间】:2014-08-23 04:52:43
【问题描述】:
我正在尝试查找元素,但出现错误 这是我的代码:
driver.get(baseURL);
driver.manage().timeouts().implicitlyWait(10 ,TimeUnit.SECONDS);
driver.manage().window().maximize();
//String parentHandle=driver.getWindowHandle();
driver.findElement(By.linkText("Create Account")).click();
System.out.println(driver.getCurrentUrl());
//String currentWindow=driver.getWindowHandle();
//driver.switchTo().window(currentWindow);
//String currentURL=;
//(currentURL);
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
driver.findElement(By.xpath("//html/body/div/div[1]/div[1]/div/div/form/div[1]/input")).sendKeys("9051902811");
driver.close();
//driver.switchTo().window(parentHandle);
}
catch(NoSuchElementException nsee){
System.out.println(nsee.toString());
}
System.exit(0);
}
我得到了例外:
无法定位元素 "method":"xpath","selector":"//html/body/div/div[1]/div[1]/div/div/form/div[1]/input "} 命令持续时间或超时:89 毫秒
请帮忙...
【问题讨论】:
-
您能否显示您尝试访问的字段的 HTML?在尝试查找元素之前也让它休眠 2 秒,selenium 有时可以快速进入
-
@Decypher 这是可怕的建议。Selenium 有implicit and explicit waits 是有原因的。 OP - 尽管显示您尝试与之交互的字段的 HTML
-
隐式等待有时会很快,就像有时睡眠一样。这是 selenium 的一个已知问题,将计时器调高只是为了调试