java - org.openqa.selenium.NoSuchElementException:无法定位元素错误
问题描述
我使用 Internet Explorer 和 Firefox 使用 Selenium Web 驱动程序在 JAVA 中编写了以下代码。每次我遇到同样的错误。尝试使用“id”和“xpath”方法,但仍然失败。尝试添加一些延迟也,仍然不起作用。
我的 Firefox JAVA 代码:
package ieconnector;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class FireFoxConnector {
public static void main(String[] args) {
try{
GetBrowserProperty gbp = new GetBrowserProperty();
System.setProperty("webdriver.ie.driver",gbp.getIeConnection());
System.setProperty("webdriver.gecko.driver","D:\\softwares\\Selenium\\geckodriver-v0.21.0-win64\\geckodriver.exe");
WebDriver wb = new FirefoxDriver();
Capabilities caps = ((RemoteWebDriver) wb).getCapabilities();
System.out.println("Caps is "+caps);
wb.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//wb.navigate().to("https://somewebsite.com:22222/SSO/ui/SSOLogin.jsp");
wb.get("https://somewebsite.com:22222/SSO/ui/SSOLogin.jsp");
wb.manage().deleteAllCookies();
wb.manage().window().maximize();
//wb.findElement(By.id("usertxt")).sendKeys(("user").toUpperCase());
//wb.findElement(By.className("passtxt")).sendKeys("password");
//WebDriverWait wait = new WebDriverWait(wb,10);
//WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("usertxt")));
wb.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
//wb.findElement(By.id("usertxt")).sendKeys("USER");
wb.findElement(By.xpath("//*[@id='usertxt']")).sendKeys("USER");
System.out.println("Testing is successful");
} catch (Exception e) {
e.printStackTrace();
}
}
}
以下是我在 IE/Firefox 中的开发者工具中的 HTML 代码截图。
解决方案
根据您共享的 HTML 以找到User ID字段,您可以使用以下解决方案:
cssSelector
:wb.findElement(By.cssSelector("input.txtbox#usertxt")).sendKeys("USER");
xpath
:wb.findElement(By.xpath("//input[@class='txtbox' and @id='usertxt']")).sendKeys("USER");
推荐阅读
- sapui5 - SAPUI5 容器集群 sap.ui.vbm.Geomap。怎么做?
- laravel - Laravel 多次检索已删除的用户
- windows - 如何在 Windows 10 上显示 Android Studio IDE 颜色
- python - Tensorflow GRU 层调用()参数——TypeError:调用()得到了一个意外的关键字参数“reset_after”
- c - 无法保存从 ESP32Cam 模块拍摄的正确 jpg 格式图片,并且无法在 Windows 中打开
- java - 无法找到请求目标的有效证书路径 (SSLHandshakeException)
- entity-framework-core - 使用 FORMAT 命令转换 SQL 查询以在实体框架核心中使用
- python - 如何使用 Python 从相机 (Nikon D5600) 读取图像文件
- maven - 无法解析 gmaven 插件中的类 util
- python - 如何使用python从子目录下的文件名中删除多个字符?