我正在使用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());
}
}
在HtmlUnitDriver()中,javascript未啓用。嘗試將驅動程序初始化爲 - WebDriver driver = new HtmlUnitDriver(true); – Deepak 2014-11-06 05:39:25
嘿,我傳遞給了構造函數,現在當我使用提交時它不會給我一個錯誤,但它仍然不會將我帶到新頁面。 – lsnow2017 2014-11-06 21:48:19