2017-08-28 15 views
-1

找到元素,我得到这个异常或错误时,我朗姆酒我的脚本:无法在webdriver的

“找不到元素:* [名称=‘密码’]”

我有尝试不同的定位器,但每次我得到相同的错误。

这里是我的脚本

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


    } 
+0

你得到的错误是什么? – Kapil

+0

我已添加完整的脚本试试这个@nirmala – iamsankalp89

回答

-1

试试这个脚本是workig罚款:

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

试试这个@Nirmala在你的main()函数中工作正常 – iamsankalp89

+0

它适用于你吗? – iamsankalp89

+0

我已经尝试过在Chrome中正常工作,但不在Firefox中。我使用的是硒3.4.0,geckodriver-v0.16.0-win32和firefox 47。安装时可能有问题吗? – nirmala

-1

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

您可以使用下面的代码类似如下─

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

上面的代码是在Java中。

+0

为什么downvote ?任何具体原因? – Kapil

0

下面是代码块定位password场和发送短信到password场的URL 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

用于查找“密码”元素,并输入密码,这行代码

代码如下:

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

此代码是否适合您? – Prashant1992

+0

谢谢我的问题已解决我的安装问题与其在Chrome上的工作有关 – nirmala