[英]Error in JavaScript while using in Cucumber Selenium with Ruby
I am trying to select a dropdown using the below code in Selenium ruby with cucumber framework.I have included a Javascript to select the value. 我正在尝试使用带有黄瓜框架的Selenium ruby中的以下代码选择一个下拉列表。我包括一个Javascript以选择值。
When(/^user selects year as (\d+)$/) do |arg1|
$driver.execute_script('document.getElementsByName("param[start_year]")[0].value=arg1;')
end
But I am getting an error like this.Please help me resolve 但是我遇到这样的错误。请帮助我解决
arg1 is not defined (Selenium::WebDriver::Error::JavascriptError)
The bit of JavaScript you are executing has the variable arg1
hard coded in. The script will see this as a JavaScript variable - and as no such variable has been defined within the JavaScript space, you get an error. 您正在执行的JavaScript的位中已硬编码了变量
arg1
。脚本会将其视为JavaScript变量-由于JavaScript空间中未定义此变量,因此会出现错误。
From the rest of the code, I assume you actually want to pass the value of the arg1
variable defined in the Ruby space, into the JavaScript code. 在其余的代码中,我假设您实际上想将Ruby空间中定义的
arg1
变量的值传递到JavaScript代码中。 I'd suggest you try this: 我建议您尝试一下:
When(/^user selects year as (\d+)$/) do |arg1|
$driver.execute_script(%Q<document.getElementsByName("param[start_year]")[0].value="#{arg1}";>)
end
Note that I've used the %Q
text delimiter so that #{}
text insertion can be combined with text including double quotes ( "
) 请注意,我使用了
%Q
文本定界符,以便可以将#{}
文本插入与包含双引号( "
)的文本组合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.