2

I am using javascript for selenium selenium-webdriver.

My code is:

        var chromeCapabilities = webdriver.Capabilities.chrome();
        this.driver = new webdriver.Builder()
            .forBrowser('chrome')
            .withCapabilities(chromeCapabilities)
            .build();

.
.
.

        driver.executeScript('return document.links;');

I dont know, how to return value from execute script.

console.log(driver.executeScript('return document.links;')); not working.

Thank you for any help

11
  • Take a look at the api docs: seleniumhq.github.io/selenium/docs/api/javascript/index.html Commented Sep 28, 2017 at 18:59
  • Where is something about function executeScript ? Did you found something? If YES tell me where ;) Commented Sep 28, 2017 at 19:01
  • You're right. I could not find it there. Commented Sep 28, 2017 at 19:16
  • I found only this: seleniumhq.github.io/selenium/docs/api/javascript/module/… but i still don't know how to use it. Commented Sep 28, 2017 at 19:19
  • And when you use the JavascriptExecutor. Like so: JavascriptExecutor js = (JavascriptExecutor)driver; String Title = js.executeScript('return document.title;').ToString(); Commented Sep 28, 2017 at 19:21

2 Answers 2

1

please try this

import org.openqa.selenium.JavascriptExecutor;

public Object executeJavascript() {
    return ((JavascriptExecutor) driver).executeScript('return document.links;');
}
Sign up to request clarification or add additional context in comments.

3 Comments

It is not javascript.
for javascript you can use driver.executeScript('return document.links;').then(function(str) {console.log(str);});
link here are more example
0
   const textPromise = driver.findElement(webdriver.By.xpath(xUserDataTF));
        textPromise.then((elem) => {
        driver.executeScript('arguments[0].scrollIntoView(true);', textPromise);
    }).catch(function(error) {
        logEverywhere('some error occured while finding userdata textfield');
      });

I am using it like this. Check out whether this works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.