错误"no such element: Unable to locate element"



我在这个页面上

https://login.alibaba.com/?spm=a2700.8293689.0.0.NdeZUw&tracelog=hd_signin

并尝试通过以下 Web 元素代码访问电子邮件字段:

public static WebElement Email_Field(WebDriver driver) throws InterruptedException {
//element = (new WebDriverWait(driver, 10)).until(ExpectedConditions
//.visibilityOfElementLocated(By.xpath("//input[@id='fm-login-id']")));
element = driver.findElement(By
.xpath("//input[@id='fm-login-id']"));
while (!isDisplayed(element)) {
Thread.sleep(3000);
System.out.println("Element is not visible yet");
}
return element;
}

public static boolean isDisplayed(WebElement element) {
try {
if(element.isDisplayed())
return element.isDisplayed();
}catch (NoSuchElementException ex) {
return false;
}
return false;
}

但出现以下异常:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='fm-login-id']"}
(Session info: chrome=59.0.3071.115)

电子邮件字段位于框架内。在访问框架中的任何元素之前,您必须切换。请尝试以下代码。

public static WebElement Email_Field(WebDriver driver) throws InterruptedException {
WebElement element;
(new WebDriverWait(driver, 30)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("alibaba-login-box"));
element = driver.findElement(By.xpath("//input[@id='fm-login-id']"));
while (!isDisplayed(element)) {
Thread.sleep(3000);
System.out.println("Element is not visible yet");
}
return element;
}

最新更新