您好我正在尝试获取一个脚本,该脚本将填写网站上的登录详细信息并提交表单,并允许我从下一页收集信息。到目前为止,我已设法填写表格,但我无法提交。我尝试了按钮上的.click并提交()但是都没有工作。所以现在我转向JavaScript来激活按钮。不幸的是,这给了我下面的错误。我试图覆盖错误,但我可能做错了。有谁知道如何提交表格?
这是我认为我的代码停止的错误
WARNING: Obsolete content type encountered: 'application/x-javascript'.
Exception in thread "main" org.openqa.selenium.WebDriverException: com.gargoylesoftware.
htmlunit.ScriptException: ReferenceError: "console" is not
defined. (https://extbasicph05.podc.sl.edst.ibm.com/FFAMobileRelay/js/ion.sound.min.js#2)
这是我的代码
public class Main {
public class MyHtmlUnitDriver extends HtmlUnitDriver {
@Override
protected WebClient modifyWebClient(WebClient client) {
//currently does nothing, but may be changed in future versions
WebClient modifiedClient = super.modifyWebClient(client);
modifiedClient.getOptions().setThrowExceptionOnScriptError(false);
return modifiedClient;
}
}
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver(true);
driver.get("url");
WebElement usr = driver.findElement(By.id("username"));
WebElement psd = driver.findElement(By.id("password"));
WebElement btt = driver.findElement(By.id("btnLogin"));
usr.sendKeys("username");
psd.sendKeys("password");
String eventScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('"
+ "click"
+ "', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('"
+ "onclick"+ "');}";
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(eventScript, btt);
System.out.println("Page title is: " + driver.getTitle());
}
}