TWhite
TWhite

Reputation: 738

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. 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"]"

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)

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:

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. 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. Any help is greatly appreciated.

Upvotes: 1

Views: 3700

Answers (1)

Janik Zikovsky
Janik Zikovsky

Reputation: 3256

I think the error is in escaping your JSON string:

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':{}}\"")

Upvotes: 2

Related Questions