How to return value from JavaScript executor in Selenium?

How to get text of WebElement using JavascriptExecutor?

I am working on a web application.

Here I have a input box where I need to get the value.
. So, here I am using "document.getElementById("idname")". So, I am getting the input box value which is "Sachin Tendulkar". But, I am unable to print the text of that input box by doing this:
JavascriptExecutor executor = (JavascriptExecutor)driver;. String text = executor.executeScript("return document.getElementById('idname').innerHTML;").toString();
System.out.println(text);
Here, "text" is not getting printed. I don't know what is the issue here. Please help me.

Use .getText() to get text from an element with text.

Try this -. JavascriptExecutor executor = (JavascriptExecutor)driver;. String text = executor.executeScript("return document.getElementById('idname').getText();").toString();
System.println(text); This is because -. The return document.getElementById('idname').innerHTML; returns the HTML code.

You should use getText() method to get the text from an element.

How to get element by JavaScript executor?

How to get an element by JavaScript executor?

I have a code that I run the script when the page load. My code: function getElement(). When the page load, but it doesn't work. I have an idea it's because the script running time. Is there anyway to fix this? Please tell me and show me how it can be done. The getElementByID() function takes an ID of an element as its argument. Your variable el in your code is just a reference to a global object that is the HTMLDocument (or window if running client side scripts) If you want to get the element with the id of 'elementId' in the context of a document you can use: var el = document.getElementById("elementId"); To do this for an element you're hovering over, you'll need to listen for the mouseenter event and run your function: var element = document.getElementById('elementId');

How to get attribute value using JavascriptExecutor?

I am trying to do some web scraping and need to get the value of the 'price' attribute.

I have a method that uses JavascriptExecutor to pass the webpage. When I use "document.documentElement.innerHTML" it shows me the whole page html. But if I try to just use ".price" it does not work. Any suggestions?
Here is the code I am using: public void getAttributeValue(String attribute). You don't need to call innerHTML on the document to get its price, you need to call innerText. Here's an example: driver.findElement(By.

Related Answers

What is JavaScript executor in Selenium?

Currently we are automating our web applications us...

How to execute JavaScript code in Selenium Python?

As per Selenium website, I have gone through it. I...

What is JavaScript extension?

I am working on an experimental project that uses JavaScript...