I'm trying to run webdriver code from java. I copied document.getElementByXpath() in a string(I have static xpath so I can live without xpath variable)String Script = "var a = this.evaluate('//div[@guidedhelpid='sharebutton']', this, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); if (a.snapshotLength > 0) { return a.snapshotItem(0); };";
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(Script);but it gives me syntax error.SyntaxError: missing ) after argument list--Mal
I'm trying to do something similar but I'm getting a syntax error. Any ideas? If I run the xPath query directly in the browser console it finds what I would expect.. just having trouble running it through Javascript.Uncaught SyntaxError: Unexpected identifierHere is the code I'm using -- postNum being an integer variable:JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("var postElement document.evaluate('(//div[@class='vcell link post_bar'])["+postNum+"]//a[@class='vcell link edit']', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); if (a.snapshotLength > 0) { return a.snapshotItem(0); };" );
Finding elements by CSS selectors in JavaScript, on the other hand, is easily supported by all modern browsers. The function you're looking for is "document.querySelector()" for single elements and "document.querySelectorsAll()" for multiple elements. In point of fact, when people tell me they're having trouble using findElement(By.cssSelector()) in WebDriver, the first thing I recommend is trying their selector in the JavaScript console using document.querySelector().