为什么Selenium驱动程序无法识别Facebook登录页面的ID元素? [英] Why Selenium driver fail to recognize ID element of Facebook login page?

查看:117
本文介绍了为什么Selenium驱动程序无法识别Facebook登录页面的ID元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始通过在线课程学习Selenium Webdriver.

I am just started to learn Selenium webdriver in an online course.

由于我是测试和Java编程的初学者,所以请帮我.

Since I am a beginner to both testing and Java programming so kindly help me out.

我试图运行自动化代码以在Facebook登录页面中自动填写用户名.最初运行该程序已成功输出.

I was trying to run the automation code to fill the username automatically in Facebook Login page. Initial running the program had successful output.

但是在第二次尝试时,我遇到了以下错误

But on the second attempt, I faced this below error

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"email"}   

此后,我用Google搜索并找到了问题 试图在我的代码中复制此 answer .

After that, I Googled and found this SO Question Tried to copy this answer in my code.

public class Helloworld {

   public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver","C:\\Seenu\\Selenium\\Driver"
        +"\\Chromedriver\\chromedriver.exe");
        WebDriver drive = new ChromeDriver();
        drive.get("https://www.facebook.com");
        // part copied from other SO question
        //Copied code starts here with little modification
        List<WebElement> elements = drive.findElements(By.id("email"));     
        if(elements.size() > 0)
        {
             System.out.println(elements.get(0).getText());
        }
        //Copied code ends here.
        else
        {
              elements.get(0).sendKeys("username@gmail.com");
              System.out.println("Username successfully entered");
        }
   }
}

但是,我得到这个错误以下消息.

But,I am getting this below error.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Basic.Helloworld.main(Helloworld.java:40)

我知道索引超出范围错误.

我的问题是Selenium驱动程序为什么无法识别Facebook登录页面的id元素

My question is why Selenium driver fails to recognize id element of Facebook login page

你们能请我解决这个问题吗?

Can you guys please me out to solve this.

推荐答案

要登录 Facebook ,您可以使用以下代码块:

To login into Facebook you can use the following code block :

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver =  new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.cssSelector("input#email")).sendKeys("Selenium");
driver.findElement(By.cssSelector("input[name='pass']")).sendKeys("Automation");
driver.findElement(By.cssSelector("input[value='Log In']")).click();

这篇关于为什么Selenium驱动程序无法识别Facebook登录页面的ID元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆