【问题标题】:Unable to find element in webdriver无法在 webdriver 中找到元素
【发布时间】:2017-08-28 06:26:02
【问题描述】:

朗姆脚本时出现此异常或错误:

“无法定位元素:*[name='password']”

我尝试过使用不同的定位器,但每次都遇到相同的错误。

这是我的脚本

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.chrome.ChromeDriver;


public class TestGmail {
    public static void main(String[] args){
        System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.16.0-win32\\geckodriver.exe");

        WebDriver driver=new FirefoxDriver();

        //System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver_win32\\chromedriver.exe");

        //WebDriver driver=new ChromeDriver();
        driver.get("https://accounts.google.com/");
        driver.findElement(By.id("identifierId")).sendKeys("myAddress");
        driver.findElement(By.cssSelector("span.RveJvd.snByac")).click();


        driver.findElement(By.name("password")).sendKeys("myPassword");

        driver.findElement(By.className("RveJvd snByac")).click();

        driver.close();
    }


   }

【问题讨论】:

  • 您遇到的错误是什么?
  • 我添加了完整的脚本试试这个@nirmala

标签: selenium selenium-webdriver webdriver geckodriver


【解决方案1】:

这是定位 password 字段并将文本发送到 url https://accounts.google.com/ 上的 password 字段的代码块

package demo;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GMAIL_LOGIN_FIREFOX_CSS 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        String url = "https://accounts.google.com/signin";
        driver.get(url);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
        driver.findElement(By.cssSelector("#identifierId")).sendKeys("your_email");
        driver.findElement(By.cssSelector(".ZFr60d.CeoRYc")).click();
        WebElement password = driver.findElement(By.cssSelector("input[class='whsOnd zHQkBf'][type='password']"));
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.elementToBeClickable(password));
        password.sendKeys("your_password");
    }
}

【讨论】:

    【解决方案2】:

    试试这个脚本,它工作得很好:

    WebDriver driver = new FirefoxDriver();
    driver.get("https://accounts.google.com/");
    driver.findElement(By.id("identifierId")).sendKeys("myAddress");
    driver.findElement(By.cssSelector("span.RveJvd.snByac")).click();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.findElement(By.name("password")).sendKeys("myPassword");
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//span[@class='RveJvd snByac']")).click();
    driver.close();
    

    【讨论】:

    • 在你的 main() 函数中试试这个@Nirmala 效果很好
    • 我已经尝试过它在 chrome 中运行良好,但在 Firefox 中却没有。我正在使用 selenium 3.4.0、geckodriver-v0.16.0-win32 和 firefox 47。安装会有什么问题吗?
    • 你在 Firefox 中遇到了什么问题。你加壁虎司机了吗
    • 将浏览器更新到 Firefox 53.0 我猜这是个问题
    • "addons.manager ERROR startup failed: [Exception..."Component returned failure code: 0x80070057" 我之前忽略了这个,因为它正在识别用户名和下一步按钮。
    【解决方案3】:

    我建议您使用显式等待。使用隐式等待是一种不好的做法。

    您可以使用下面的代码,如下所示-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
    

    以上代码是Java。

    【讨论】:

      【解决方案4】:

      使用此代码行查找“密码”元素并输入密码

      代码如下:

      driver.findElement(By.Xpath("html/body/div/div/div[2]/div[2]/form/div[2]/div/div/div[1]/div[1]/div/div[1]/div/div[1]/input")).sendKeys("Your password ")
      

      【讨论】:

      • 谢谢你我的问题已经解决了我在chrome上安装时遇到问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 2014-07-13
      • 2016-07-02
      • 1970-01-01
      相关资源
      最近更新 更多