简体   繁体   English

org.openqa.selenium.NoSuchElementException:无法找到元素错误

[英]org.openqa.selenium.NoSuchElementException: Unable to locate element error

I have written the following code in JAVA using Selenium web driver using both Internet Explorer and Firefox. 我已经使用Internet Explorer和Firefox使用Selenium Web驱动程序在JAVA中编写了以下代码。 Everytime I am getting the same error. 每当我遇到相同的错误时。 Tried using both "id" and "xpath" method, but still it is failing. 尝试同时使用“ id”和“ xpath”方法,但仍然失败。 Tried adding some delay also, still does not work. 尝试添加一些延迟也仍然无法正常工作。

My JAVA code for Firefox: 我的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();
}

}

} }

And the following is a screenshot of the HTML code in my developer tool in IE/Firefox. 以下是我在IE / Firefox中的开发人员工具中的HTML代码的屏幕截图。

HTML

As per the HTML you have shared to locate the User ID field you can use the following solution: 根据您共享的HTML来找到“ 用户ID”字段,您可以使用以下解决方案:

  • cssSelector : cssSelector

     wb.findElement(By.cssSelector("input.txtbox#usertxt")).sendKeys("USER"); 
  • xpath : xpath

     wb.findElement(By.xpath("//input[@class='txtbox' and @id='usertxt']")).sendKeys("USER"); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.openqa.selenium.NoSuchElementException:无法找到元素: - org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:无法找到元素:-搜索字段 - org.openqa.selenium.NoSuchElementException: Unable to locate element:- Search field 无法找到元素:org.openqa.selenium.NoSuchElementException - Unable to locate element: org.openqa.selenium.NoSuchElementException webdriver org.openqa.selenium.NoSuchElementException:无法找到元素: - webdriver org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:无法找到元素: - org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element error using Selenium through Java - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element error using Selenium through Java org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:无法找到表格标签内的元素 - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: not able to locate the element inside table tag 代码中的 Selenium Fluent Wait Implementation 仍然给出“org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:” - Selenium Fluent Wait Implementation in the code still gives “org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:” org.openqa.selenium.NoSuchElementException:在使用 Selenium 和 Java 定位元素以提取文本时无法定位元素错误 - org.openqa.selenium.NoSuchElementException: Unable to locate element error while locating an element to extract the text using Selenium and Java org.openqa.selenium.NoSuchElementException:没有这样的元素:尝试使用Java通过Selenium登录时无法找到元素 - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element while trying to login through Selenium with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM