gpt4 book ai didi

javascript - WebDriver JavascriptExecutor 不工作

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:43 25 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

我们发现了问题所在。我们使用 IE 9 和文档 here指出如果 <!DOCTYPE html>标签不存在,IE 9 默认情况下将以 Quriks 模式(也称为兼容模式)显示页面。

通过添加 <!DOCTYPE html>标签将使页面呈现为 IE 9 并且不使用 Quirks 模式。您可能还必须包含 meta标签<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9"> meta如果需要,标签还允许您通过执行 content="IE=EmulateIE# 来模拟特定版本的 IE。或content=IE#

我们遇到的问题是旧版本的 IE 不支持我们使用的 JavaScript 方法。我们尝试并尝试将一些方法更改为较旧的样式,但我们不断遇到越来越多的旧 IE 版本不支持的方法的兼容性问题。

关于javascript - WebDriver JavascriptExecutor 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31546586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com