org.openqa.selenium.WebDriverException:未知错误:Runtime.evaluate引发异常:SyntaxError:无效或意外的令牌

时间:2017-07-14 06:01:43

标签: javascript java selenium

我正在尝试在Selenium网络驱动程序中运行以下代码,但它显示了一些错误

代码是

    WebElement w=driver.findElement(By.xpath("//*[@class='tab']"));

    JavascriptExecutor js=(JavascriptExecutor) driver;`

    js.executeScript("arguments[0].setAttribute('disable,'');",w);

错误是:

org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Invalid or unexpected token

(Session info: chrome=59.0.3071.115)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.10240 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 463 milliseconds
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
System info: host: 'DESKTOP-AQGDP71', ip: '192.168.2.25', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\SEKAR\AppData\Local\Temp\scoped_dir3836_13558}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.115, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 9568bb1918bcb9bfdfbc4afab2cf8294
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:540)
    at `enter code here`stepdefinition.Cheking`enter code here`.search_with_text_and_check_listed_corectly(Chek`enter code here`ing.ja`enter code here`va:57)
    at ?

使用文本搜索并检查corectly(Cheking.feature:7)

2 个答案:

答案 0 :(得分:0)

你缺少引号。可能是一个错字。

js.executeScript("arguments[0].setAttribute('disable','');",w);

答案 1 :(得分:0)

我也遇到了这个问题,就我而言,我使用的是:

element.sendKeys("5'7");

如果您要发送的数据中有单引号,它前面需要有反斜杠。 您可以使用以下 java 代码:

String text = "5'7";
text = text.replace("'", "\\'");
element.sendKeys(text);

现在,它会将数据发送为“5'7”而不是“5'7”。