您好我正在尝试找到一个带有以下代码的文本框。忘记找不到元素异常。
List<WebElement> productivityButtons = driver.findElements(By.className("app-open-btn"));
for (WebElement item : productivityButtons) {
String appName = item.getAttribute("appName");
//System.out.println(item.getText());
//System.out.println(item.getAttribute("appName"));
//System.out.println(item.getAttribute("appName"));
if (item.getAttribute("appName").equals("ReceivingSstkClient")) {
System.out.println("*****matching");
System.out.println(item);
item.click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[id='inputUPCNumber']")).sendKeys("5900020010000");
//driver.findElementById("inputUPCNumber").sendKeys("5900020010000");
//driver.findElement(By.cssSelector("button button-icon button-clear ion-navicon")).click();
Thread.sleep(2000);
答案 0 :(得分:0)
编辑:要在尝试查找元素之前切换窗口,您需要一些类似下面的代码:
// This is before you click the button to launch the new window and assumes you currently only have one window open
String currentWindow = driver.getWindowHandle();
// This is where you click the button to open the "application" window
button.click();
// Now the new window is open we loop through all open browser windows, and find the one that is **not** the window we already had open, then switch to it
for (String handle : driver.getWindowHandles()){
if (!handle.equals(currentWindow))
driver.switchTo().window(handle);
}
// Now you can proceed with finding the element you want
发布相关HTML后,我将能够发表更多评论,但我相信您当前的代码也需要整理:
List<WebElement> productivityButtons = driver.findElements(By.className("app-open-btn"));
for (WebElement item : productivityButtons) {
String appName = item.getAttribute("appName");
if (appName.equals("ReceivingSstkClient")) {
item.click();
driver.findElement(By.id("inputUPCNumber")).sendKeys("5900020010000");
你应该摆脱Thread.sleep(2000);
,因为你有'implicitlyWait'(虽然很难说肯定不知道你的代码是什么。并且隐含的等待应该在你之后移动到行创建你的驱动对象。
答案 1 :(得分:0)
我同意Josh,你可以使用id,如下所示:driver.findElement(By.id("inputId"))
如果您想使用xpath,请尝试:driver.findElement(By.xpath("//*[@id='inputId']"))