[英]How to change value of element via JavaScript with Selenium webdriver .NET
I'm trying to test a popup page with chrome webdriver and selenium2 using .NET, but I'm having issues. 我正在尝试使用.NET使用chrome webdriver和selenium2测试弹出页面,但出现问题。 I need to change the value of an element after it's window pops up. 我需要在窗口弹出后更改元素的值。 I need to chage the default "selectedIndexes":["1"]" to "selectedIndexes":["0"]" 我需要将默认的“ selectedIndexes”:[“ 1”]“更改为” selectedIndexes“:[” 0“]”
The element is: 元素是:
<input id="tsTabs_ClientState" name="tsTabs_ClientState" type="hidden" autocomplete="off"
value="{"selectedIndexes":["1"], "logEntries":[], "scrollState":{}}">
My code is as follows: (I've tried both below listed ExecuteScript lines) 我的代码如下:(我已经尝试了下面列出的ExecuteScript行)
Dim chromeDriver = New ChromeDriver("C:\clearcase\Projects\CMS\VbSeTest")
Try
'Chrome Test
chromeDriver.Navigate().GoToUrl("http://localhost/CMS/<location>.aspx")
Dim queryC As IWebElement = chromeDriver.FindElement(By.Id("ctl00_cphM_grd_ctl00_ctl02_ctl00_ACI_btnInitInsert"))
queryC.Click()
Dim current As String = chromeDriver.CurrentWindowHandle
Dim windows = chromeDriver.WindowHandles.AsEnumerable
Dim addOrgWindow As IWebDriver
For Each window In windows
If window <> current Then
addOrgWindow = chromeDriver.SwitchTo.Window(window)
End If
Next
'chromeDriver.ExecuteScript("document.getElementById('tsTabs_ClientState').value='{'selectedIndexes':['0'],'logEntries':[],'scrollState':{}}'")
'OR
chromeDriver.ExecuteScript("var tab=$get('tsTabs_ClientState'); tab.value ='{'selectedIndexes':['0'],'logEntries':[],'scrollState':{}}'")
addOrgWindow.FindElement(By.Id("Organization_txtName")).SendKeys("MagicKingdom")
addOrgWindow.FindElement(By.Id("Organization_cbIndustry_cb_Input")).SendKeys("REP")
addOrgWindow.FindElement(By.Id("lbAdd")).Click()
chromeDriver.Quit()
Catch e As Exception
chromeDriver.Quit()
MsgBox(e.ToString())
End Try
I keep getting an error at the chromeDriver.ExecuteScript(..... line as: 我在chromeDriver.ExecuteScript(.....
Started ChromeDriver (v2.1) on port 63559
System.InvalidOperationException: unknown error: Runtime.evaluate threw exceptio
n: SyntaxError: Unexpected identifier
(Session info: chrome=28.0.1500.72)
(Driver info: chromedriver=2.1,platform=Windows NT 6.1 SP1 x86_64)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response erro
rResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecu
te, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptInternal(String script
, Boolean async, Object[] args)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object
[] args)
at VbSeTest.PopUpTest.Main() in C:\clearcase\Projects\CMS\VbSeTest\VbSeTest\P
opUpTest.vb:line 89
[8412:6008:0717/103910:ERROR:textfield.h(162)] NOT IMPLEMENTED
The javascript works in the Selenium IDE for running script to set the text field of another hidden client so I was hopping to do something similar here. javascript在Selenium IDE中可以运行脚本来设置另一个隐藏客户端的文本字段,因此我希望在此处做类似的事情。 I know it finds the element but it doesn't run the script. 我知道它找到了元素,但是它没有运行脚本。 The other Stack answers here , here , or here have not proven to be what I need to solve this issue. 其他Stack 此处 , 此处或此处的答案尚未证明是解决此问题所需要的。 Any help is greatly appreciated. 任何帮助是极大的赞赏。
I think the error is in escaping your JSON string: 我认为错误在于转义JSON字符串:
chromeDriver.ExecuteScript("var tab=$get('tsTabs_ClientState'); tab.value ='{'selectedIndexes':['0'],'logEntries':[],'scrollState':{}}'")
Instead, try: 相反,请尝试:
chromeDriver.ExecuteScript("var tab=$get('tsTabs_ClientState'); tab.value =\"{'selectedIndexes':['0'],'logEntries':[],'scrollState':{}}\"")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.