无法在webdriver中找到元素

时间:2017-08-28 06:26:02

标签: selenium selenium-webdriver webdriver geckodriver

当我朗读我的剧本时,我得到了这个例外或错误:

  

"无法找到元素:* [name ='密码']"

我尝试过不同的定位器,但每次遇到同样的错误。

这是我的剧本

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();
    }


   }

4 个答案:

答案 0 :(得分:0)

以下是用于找到password字段并将文本发送到网址password

上的https://accounts.google.com/字段的代码块
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");
    }
}

答案 1 :(得分:-1)

试试这个脚本是好的工作:

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();

答案 2 :(得分:-1)

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

您可以使用下面的代码 -

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

上面的代码是Java。

答案 3 :(得分:-1)

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

代码如下:

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 ")