Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium WebDriver找不到WebElements_Java_Css_Xpath_Selenium Webdriver - Fatal编程技术网

Java Selenium WebDriver找不到WebElements

Java Selenium WebDriver找不到WebElements,java,css,xpath,selenium-webdriver,Java,Css,Xpath,Selenium Webdriver,下面我编写的脚本导航到所需的URL,但它没有输入用户名和密码的值。这两个字段的Xpath和css都是正确的。知道这里出了什么问题吗 org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class websiteClass { public static void main(String[] args)

下面我编写的脚本导航到所需的URL,但它没有输入用户名和密码的值。这两个字段的Xpath和css都是正确的。知道这里出了什么问题吗

org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class websiteClass {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://opensource.demo.orangehrmlive.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.findElement(By.xpath(".//*[@id='txtUsername']")).sendKeys("Admin");
        driver.findElement(By.xpath(".//*[@id='txtPassword']")).sendKeys("admin");      
        driver.findElement(By.xpath(".//*[@id='btnLogin']")).click();
    }

}

在geckodriver 0.18版本中,driver.manage().window().maximize()抛出异常(实际上是一个异常)

删除两行后:

   driver.manage().window().maximize();
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
代码对我来说很好(程序使用geckodriver成功登录到网站)

还要确保设置了webdriver系统属性(在
webdriver driver=new FirefoxDriver()之前;

我还尝试了你的代码,没有对ChromeDriver做任何更改,效果很好


如果您仍然有问题,请发布您的geckodriver版本和异常堆栈跟踪。

它在Firefox 43.0和2.51中运行良好,我想这是Firefox浏览器的问题。可以将gecko驱动程序与selenium 3.3 JAR配合使用,也可以使用Firefox 43.0和2.51 JAR selenium

如果您使用chrome并更新chromedriver,请使用此选项。对于Firefox,您只需要更改setproperty值

System.setProperty("webdriver.chrome.driver", "E:\\software and tools\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
            driver.get("http://opensource.demo.orangehrmlive.com/");
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            driver.findElement(By.xpath(".//*[@id='txtUsername']")).sendKeys("Admin");
            driver.findElement(By.xpath(".//*[@id='txtPassword']")).sendKeys("admin");      
            driver.findElement(By.xpath(".//*[@id='btnLogin']")).click();

            driver.close();

谢谢你,瓦西里。当我将geckodriver版本从0.11更新到0.18时,代码运行得非常完美。
System.setProperty("webdriver.chrome.driver", "E:\\software and tools\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
            driver.get("http://opensource.demo.orangehrmlive.com/");
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            driver.findElement(By.xpath(".//*[@id='txtUsername']")).sendKeys("Admin");
            driver.findElement(By.xpath(".//*[@id='txtPassword']")).sendKeys("admin");      
            driver.findElement(By.xpath(".//*[@id='btnLogin']")).click();

            driver.close();