1

I have got a problem with obtaining some href's by Selenium Webdriver. Therefore I have used JQuery script to make it.

script = 
"(function() {
    var a = [];
    $("a.class0.class1.link").each(function() {
        a.push($(this).attr('href'));
    });
    return a;
})()";

executed by such code:

result = javascriptExecutor.executeScript(script)

But result has null value. When I run that script in Chrome console I have got correct result - array of href's. What I did wrongly?

1 Answer 1

2

Ok, I have solved problem. There are 2 ways of achieving result - obtaining href's from links.

script = "return $('a.class0.class1.link');"

execute such script and get an Array of WebElements from which it is possible to get attributes:

val js = browser.asInstanceOf[JavascriptExecutor]
val scriptResult = js.executeScript(script)
val result = ListBuffer.empty[String]

scriptResult.asInstanceOf[util.ArrayList[WebElement]].forEach(x => result +=
  x.getAttribute("href"))

result.toList

or in second way - execute such JQuery code:

val script = "return (function() {var table = [];$('a.class0.class1.link').each(function() { table.push($(this).attr('href'));});return table;})();"

and collect result:

val js = browser.asInstanceOf[JavascriptExecutor]
val scriptResult = js.executeScript(javascript)
scriptResult.asInstanceOf[util.ArrayList[String]].asScala.toList
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to come back and mark this answer as accepted so the question doesn't get left unanswered.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.