我试图在Selenium WebDriver中使用JavaScript填写网页上的文本框。一个同事this github存储库在线填写文本框以加快速度。
当我运行程序时,它将在((JavascriptExecutor)driver).executeScript("(function( window ) { 'use strict';"
处抛出错误,然后它会在每个文本框中移动,但它不会将文本添加到框中。我和我的同事昨天看了很久,比较了我们的代码,并且无法弄清楚出了什么问题。这是我的代码及其产生的错误。
感谢任何帮助。
Java代码
InputStream inputStream = ClientInformationComponent.class.getResourceAsStream("AutoFillClientInsuredComponent.js");
StringWriter writer = new StringWriter();
try
{
IOUtils.copy(inputStream, writer, "UTF-8");
}
catch (IOException e)
{
e.printStackTrace();
}
for (ClientDAO client : clients)
{
String script = writer.toString();
try
{
if (driver instanceof JavascriptExecutor)
{
//ERROR IS THROWN HERE webdriver.components.ClientInformationComponent.fill(ClientInformationComponent.java:128)
((JavascriptExecutor)driver).executeScript("(function( window ) { 'use strict';"
+ "var lastName = '" + client.getLastName() + "';"
+ "var firstName = '" + client.getFirstName() + "';"
+ "var middleName = '" + client.getMiddleName() + "';"
+ "var suffix = '" + client.getSuffix() + "';"
+ "var streetAddress = '" + client.getMemberAddress() + "';"
+ "var city = '" + client.getMemberCity() + "';"
+ "var zipCode = '" + client.getMemberZipCode() + "';" + "var birthDate = '" + client.getDateOfBirth() + "';"
+ "var homePhoneNumber = '" + client.getHomePhone() + "';"
+ script);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
JavaScript文件
var document = window.document,
fieldValueMap = {
"LastName" : lastName,
"FirstName" : firstName,
"MiddleName" : middleName,
"Suffix" : suffix,
"MemberAddress" : streetAddress,
"MemberCity" : city,
"MemberZipCode" : zipCode,
"MemberZipCodeSuffix" : memberZipCodeSuffix,
"DateOfBirth" : birthDate,
"HomePhone" : homePhoneNumber
};
Object.keys( fieldValueMap ).forEach(function( name ){
var input = document.querySelector( "form input[name='" + name + "']" )
|| document.querySelector( "form select[name='" + name + "']" )
|| document.querySelector( "form textarea[name='" + name + "']" );
input && input.type !== "hidden" && ( input.value = fieldValueMap[ name ] );
});
})( window );
错误记录
org.openqa.selenium.WebDriverException: JavaScript error (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 47 milliseconds
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
System info: host: 'COL-ISD-D56345', ip: '10.8.11.30', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_65'
Session ID: 10b4929d-001c-412c-94bc-ce9d006b6dd0
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=accept, version=9, ie.usePerProcessProxy=false, ignoreProtectedModeSettings=false, cssSelectorsEnabled=true, requireWindowFocus=false, initialBrowserUrl=http://localhost:22000/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
Command duration or timeout: 356 milliseconds
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: 'COL-ISD-D56444', ip: '10.8.12.163', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=9, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, webdriver.remote.sessionid=863d35b9-ff0d-4b83-b4e0-983d9272353d, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:22000/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=accept}]
Session ID: 863d35b9-ff0d-4b83-b4e0-983d9272353d
答案 0 :(得分:0)
我们发现了问题所在。我们正在使用IE 9和文档here指出,如果<!DOCTYPE html>
标签不存在,IE 9将以 Quriks模式显示页面,也称为兼容性模式默认情况下。
通过添加<!DOCTYPE html>
标记将使IE 9的页面呈现而不使用Quirks模式。您可能还必须添加meta
代码<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
meta
标记还允许您根据需要模拟特定版本的IE,执行content="IE=EmulateIE#
或content=IE#
我们遇到的问题是旧版本的IE不支持我们使用的JavaScript方法。我们经历了尝试将一些方法更改为旧样式,但是我们不断遇到与旧版IE中不支持的方法相关的越来越多的兼容性问题。