2017-08-02 86 views
-1

我在驅動程序中使用硒的PhantomJS。在這裏,我發現插入電子郵件地址沒有問題。但是,在點擊下一個之後,應該有一個名爲'password'的輸入標籤。但是,在等待之後,我無法獲得所需的「密碼」標籤。它顯示錯誤沒有找到元素。有時候,我收到以下錯誤(顯示堆棧跟蹤):無法找到密碼輸入對象用硒登錄谷歌

 
org.openqa.selenium.InvalidElementStateException: {"errorMessage":"Element is not currently interactable and may not be manipulated","request":{"headers":{"Accept-Encoding":" 

因爲,我已經等待了足夠的時間,下面的代碼必須工作。 這是下面的代碼片段的工作。

driver.get("https://accounts.google.com/ServiceLoginAuth"); 
    driver.findElement(By.id("identifierId")).sendKeys("xxxxxxxx"); 
    driver.findElement(By.id("identifierNext")).click(); 

    WebDriverWait wait = new WebDriverWait(driver, 20); 
    wait.until(ExpectedConditions.presenceOfElementLocated(By.name("password"))); 
    driver.findElement(By.name("password")).sendKeys("xxxxxxxx"); 
    driver.findElement(By.id("passwordNext")).click(); 

這裏是顯示了從google sign in page電子郵件輸入字段中特定的HTML:

 
input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="username" spellcheck="false" tabindex="0" aria-label="Email or phone" name="identifier" id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value="" 

下一頁按鈕id是 'identifierNext' 和HHE隱藏的密碼字段:

 
input type="password" name="hiddenPassword" jsname="RHeR4d" class="yb9KU" tabindex="-1" aria-hidden="true" 

插入郵件後點擊下一步,輸入字段爲Ë密碼是:

 
input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="current-password" spellcheck="false" tabindex="0" aria-label="Enter your password" name="password" autocapitalize="off" autocorrect="off" dir="ltr" data-initial-dir="ltr" data-initial-value="" 

現在的問題是,我已經使用等機制重新加載頁面,這樣我可以得到密碼插入頁面。但是,使用Selenium我找不到'password'的指定標籤。 我認爲點擊無法正常工作。是否有機會不工作點擊Next

+1

是密碼字段是動態的,發送電子郵件對於G郵件的完整代碼? –

+0

你爲什麼使用'By.name'作爲'password'字段,'By.id'作爲'passwordNext'字段?試試'By.id'。 – Dalton

+0

正如@santhosh指出的那樣,如果該字段是動態的,則應該使用xpath。 – Dalton

回答

0

試試這個:

driver.findElement(By.xpath("//input[1]")); 

或者這樣:

WebDriverWait wait = new WebDriverWait(driver, 100); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@type='password']"))); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@type='password']"))); 
WebElement elementpwd = driver.findElement(By.xpath("//input[@type='password']")); 

我希望它能幫助。

+0

I真的不知道..在PhantomJS中,主要問題是什麼?我不能使用上面的答案以及我的代碼得到它的工作,但是,使用FirefoxDriver,我已經成功運行代碼。 ..「元素當前不可交互,可能不會被操縱」,「請求」:{「h eaders「或者」無法用xpath查找元素...「 – amee

+0

我編輯了我的答案,提出了另一個建議。 – Dalton

0

,請查收爲使用Selenium網絡驅動器使用Chrome瀏覽器

公共類Newtest {

public static void main(String[] args) throws InterruptedException { 
    // TODO Auto-generated method stub 

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tharun\\Desktop\\work\\chromedriver.exe"); 
    WebDriver driver= new ChromeDriver(); 
    driver.get("https://www.google.com"); 
    driver.findElement(By.xpath("//*[@id='gb_70']")).click(); 
    driver.findElement(By.xpath("//*[@id=\'identifierId\']")).sendKeys("*******@gmail.com"); 
    driver.findElement(By.xpath("//*[@id=\'identifierNext\']/content/span")).click(); 
    Thread.sleep(2000); 
    driver.findElement(By.xpath("//input[@type='password']")).sendKeys("*******"); 
    driver.findElement(By.xpath("//*[@id=\'passwordNext\']/content/span")).click(); 
    Thread.sleep(2000); 
    driver.findElement(By.xpath("//*[@id=\"gbw\"]/div/div/div[1]/div[2]/a")).click(); 
    Thread.sleep(2000); 
    driver.findElement(By.xpath("//*[@id=':ir']/div/div")).click(); 
    Thread.sleep(5000); 
    driver.findElement(By.cssSelector("#\\3a og")).sendKeys("*******@gmail.com"); 
    driver.findElement(By.cssSelector("#\\3a ny")).sendKeys("This is my selenium web driver automation code "); 
    Thread.sleep(5000); 
    driver.findElement(By.xpath("//*[@id=\':p3\']")).sendKeys("This is my selenium web driver automation code "); 
    Thread.sleep(2000); 
    driver.findElement(By.xpath("//*[@id=\':no\']")).click(); 




} 

}