2014-11-06 27 views
0

我正在使用Selenium Library來嘗試登錄到我的www.marketwatch.com。它可以找到所有的元素,但在調用.submit()方法時,會出現「無法定位用於提交表單的元素」錯誤。當使用button.click()時,根本沒有任何事情發生。由於Selenium「無法找到用於提交表單的元素」

package com.logansnow.marketwatch; 

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.htmlunit.HtmlUnitDriver; 
import org.openqa.selenium.Keys; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class Main { 

    public static void main(String[] args){ 

     java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF); 
     java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); 

     WebDriver driver = new HtmlUnitDriver(); 
     driver.get("https://id.marketwatch.com/access/50eb2d087826a77e5d000001/latest/login_standalone.html?url=http%3A%2F%2Fwww.marketwatch.com%2Fuser%2Flogin%2Fstatus"); 
     WebElement email = driver.findElement(By.name("username")); 
     WebElement loginPass = driver.findElement(By.name("password")); 
     WebElement button = driver.findElement(By.id("submitButton")); 
     email.sendKeys("***********@gmail.com"); 
     loginPass.sendKeys("******************"); 
     //loginPass.submit(); 
     driver.findElement(By.className("login_submit")).click(); 
     email.submit(); 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     System.out.println(driver.getTitle()); 

    } 

} 
+1

在HtmlUnitDriver()中,javascript未啓用。嘗試將驅動程序初始化爲 - WebDriver driver = new HtmlUnitDriver(true); – Deepak 2014-11-06 05:39:25

+0

嘿,我傳遞給了構造函數,現在當我使用提交時它不會給我一個錯誤,但它仍然不會將我帶到新頁面。 – lsnow2017 2014-11-06 21:48:19

回答

0

問題似乎是從email.submit();聲明而產生。您需要真正提交表單的兩個陳述中的一個。

package com.logansnow.marketwatch; 

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.htmlunit.HtmlUnitDriver; 
import org.openqa.selenium.Keys; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class Main { 

    public static void main(String[] args){ 

     java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF); 
     java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); 

     WebDriver driver = new HtmlUnitDriver(); 
     driver.get("https://id.marketwatch.com/access/50eb2d087826a77e5d000001/latest/login_standalone.html?url=http%3A%2F%2Fwww.marketwatch.com%2Fuser%2Flogin%2Fstatus"); 
     WebElement email = driver.findElement(By.name("username")); 
     WebElement loginPass = driver.findElement(By.name("password")); 
     WebElement button = driver.findElement(By.id("submitButton")); 
     email.sendKeys("***********@gmail.com"); 
     loginPass.sendKeys("******************"); 
     //loginPass.submit(); 

     // Click on the submit button to submit the form. 
     driver.findElement(By.className("login_submit")).click(); 

     // Can also use this since you already have WebElement referencing the submit button. 
     // button.click(); 

     // Or invoke submit on the button element 
     // button.submit(); 

     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     System.out.println(driver.getTitle()); 
    } 
} 
+0

我不明白你說什麼不同。 – lsnow2017 2014-11-06 23:36:42

0

請使用以下提交。只需將您的提交代碼替換爲以下代碼:

driver.findElement(By.id(「submitButton」))。click();

0

如果您的網頁包含JavaScript,HTMLUnitDriver默認情況下不啓用JavaScript。

確保您使用構造函數參數啓用JavaScript。

WebDriver driver = new HtmlUnitDriver();