Sanjoy
Sanjoy

Reputation: 336

Execute JavaScript using Selenium Webdriver

Hi I am trying to execute a JavaScript in VBA in the native excel application using Selenium Wrapper.

The following code executes a JavaScript on Chrome Console and gives me my expected output.

$x("//label[text()[contains(.,'British')]]")[0].children[1].children[0].click()

To execute JavaScript usually it needs to be in double quotes however this is not accepted by VBA since it has two double quotes.

driver.ExecuteScript("$x("//label[text()[contains(.,'British')]]")[0].children[1].children[0].click()")

I tried to define it as a string in another variable with the concatenation of the values, but it is still giving me error.

I don't want findElementByXpath with click method. Would request if anyone knows a workaround of this.

Edit 1 instead of $x which works on chrome, using document.evaluate works in webdriver.

Upvotes: 1

Views: 5079

Answers (1)

Sam
Sam

Reputation: 5721

For what it's worth,

driver.ExecuteScript("$x("//label[text()[contains(.,'British')]]")[0].children[1].children[0].click()")

should be written like this:

driver.ExecuteScript("$x(""//label[text()[contains(.,'British')]]"")[0].children[1].children[0].click()")

Upvotes: 1

Related Questions