使用Selenium Webdriver执行JavaScript [英] Execute JavaScript using Selenium Webdriver
问题描述
我正在尝试使用Selenium Wrapper在本机excel应用程序中的VBA中执行JavaScript.
Hi I am trying to execute a JavaScript in VBA in the native excel application using Selenium Wrapper.
以下代码在Chrome控制台上执行了JavaScript,并为我提供了预期的输出.
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()
要执行JavaScript,通常需要将其用双引号引起来,但是VBA不接受,因为它有两个双引号.
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.
我不希望将clickElement用作findElementByXpath.会询问是否有人知道此解决方法.
I don't want findElementByXpath with click method. Would request if anyone knows a workaround of this.
编辑1 而不是$ x在chrome上可用,而使用document.evaluate在webdriver中可用.
Edit 1 instead of $x which works on chrome, using document.evaluate works in webdriver.
推荐答案
对于它的价值,
driver.ExecuteScript("$x("//label[text()[contains(.,'British')]]")[0].children[1].children[0].click()")
应该这样写:
driver.ExecuteScript("$x(""//label[text()[contains(.,'British')]]"")[0].children[1].children[0].click()")
这篇关于使用Selenium Webdriver执行JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!