Selenium“找不到用于提交表单的元素”

时间:2014-11-06 04:48:21

标签: java forms selenium submit

我正在使用Selenium Library尝试为我登录www.marketwatch.com。它可以找到所有元素,但是在调用.submit()方法时,我得到一个“找不到用于提交表单的元素”错误。使用button.click()时,根本没有任何事情发生。感谢

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

    }

}

3 个答案:

答案 0 :(得分: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());
    }
}

答案 1 :(得分:0)

请使用以下提交。只需用以下内容替换您的提交代码:

driver.findElement(By.id("提交按钮&#34))。单击();

答案 2 :(得分:0)

如果您的网页包含javascript,HTMLUnitDriver默认情况下不启用javascript。

确保使用构造函数参数启用javascript。

WebDriver driver = new HtmlUnitDriver();