0

Description: I am using following JS code line using chrome console (whose image is also attached) and the this code gives ids of opened Tabs in a page of my web application (tabs image is also attached to understand output). But Now I don't know how to use this JS code line in my automation script that I am writing using Selenium Web-driver in Java.

Code:

Array.from(document.querySelectorAll("[class*='x-tab x-unselectable x-tab-after-title x-box-item x-tab-default x-top x-tab-top x-tab-default-top x-closable x-tab-closable x-tab-default-closable x-closable-top x-tab-closable-top x-tab-default-closable-top x-icon-text-left x-tab-icon-text-left x-tab-default-icon-text-left']")).map(function(item){return item.id;})

Tabs screen-shot

Question:

Can anybody tell me how to use this code line in selenium Webdriver script using Java? I am using above JS code line on chrome console opened for my application and it is giving correct result.

Output of above code line using chrome console

Engineer
  • 13
  • 4
  • Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Jan 30 '18 at 14:36
  • Possible duplicate of [How to use JavaScript with Selenium WebDriver Java](https://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-java) – JeffC Jan 30 '18 at 14:36
  • Did you google this at all? A simple google of "java selenium execute javascript" returns all kinds of results that answer this question. – JeffC Jan 30 '18 at 14:38
  • Hi Jeff, thanks for comment. yes I googled it. but could not get success and that's why I asked here. and Screenshot is just the output of the above pasted code. If you can help, then please let me know the solution of that. Thanks – Engineer Jan 31 '18 at 10:48
  • Also, I have edited my question. – Engineer Feb 02 '18 at 13:01
  • @Engineer Did you try my answer? – Fenio Feb 08 '18 at 15:28

1 Answers1

0

I think you can use executeScript method like this:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", element); //click the WebElement via Javascript.

In your case, your code would look like this:

((JavascriptExecutor) driver).executeScript("return Array.from(document.querySelectorAll(\"[class*='x-tab x-unselectable x-tab-after-title x-box-item x-tab-default x-top x-tab-top x-tab-default-top x-closable x-tab-closable x-tab-default-closable x-closable-top x-tab-closable-top x-tab-default-closable-top x-icon-text-left x-tab-icon-text-left x-tab-default-icon-text-left']\")).map(function(item){return item.id;})");

executeScript returns an instance of Object, so in order to work with it, you would have to save the array in a variable and make a cast to ArrayList. You have to add return inside your javascript to tell WebDriver that this method will return something.

Try the following:

List<String> listOfIds = (List<String) ((JavascriptExecutor) driver).executeScript("return Array.from(document.querySelectorAll(\"[class*='x-tab x-unselectable x-tab-after-title x-box-item x-tab-default x-top x-tab-top x-tab-default-top x-closable x-tab-closable x-tab-default-closable x-closable-top x-tab-closable-top x-tab-default-closable-top x-icon-text-left x-tab-icon-text-left x-tab-default-icon-text-left']\")).map(function(item){return item.id;})");

I will show you a warning Type safety: Unchecked cast from Object to List<String> and I don't know how to provide a checked cast. However, this should work.

Fenio
  • 3,528
  • 1
  • 13
  • 27