如何将变量设置为activeElement?

时间:2015-04-11 20:52:01

标签: java javascript selenium

我正在将字符串设置为activeElement.value,一切正常。现在我想将一个变量设置为activeElement.value,我得到以下错误代码:

  

线程“main”中的异常org.openqa.selenium.WebDriverException:   patientID未定义命令持续时间或超时:66毫秒   构建信息:版本:'2.45.0',修订版:'5017cb8',时间:'2015-02-26   23:59:50'系统信息:主持人:'MALTE',ip:'169.254.239.149',os.name:   'Windows 7',os.arch:'x86',os.version:'6.1',java.version:   '1.8.0_40'驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver   功能[{applicationCacheEnabled = true,rotate = false,   handlesAlerts = true,databaseEnabled = true,version = 37.0.1,   platform = WINDOWS,nativeEvents = false,acceptSslCerts = true,   webStorageEnabled = true,locationContextEnabled = true,   browserName = firefox,takesScreenshot = true,javascriptEnabled = true,   cssSelectorsEnabled = true}]会话ID:   9816aed6-75f7-48e2-90c5-4c018efd0af9 at   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(未知   来源)at   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知   来自java.lang.reflect.Constructor.newInstance(未知来源)     在   org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)     在   org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)     在   org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)     在   org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:508)     在   defaultPackage.DataCapture.SelectExistingPatientByID(DataCapture.java:179)     在defaultPackage.DataCapture.main(DataCapture.java:50)引起:   org.openqa.selenium.WebDriverException:未定义patientID

带变量的代码(不工作):

String patientID;
patientID = "n1n3";
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.activeElement.value = patientID;");

带有字符串(工作)的代码部分:

   jse.executeScript("document.activeElement.value = 'n1n3';");

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您传递的是值而不是变量。请参阅下面的答案。

    String patientID;
    patientID = "n1n3";
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("document.activeElement.value = '" + patientID + "';");