There is input field which id is condition and getText()
and getAttribute()
are not working
driver.findElement(By.id("condition")).getText() //is not working..
So I tried in the console
document.getElementById("condition").value //return the value.
So I tried
JavascriptExecutor jse = (JavascriptExecutor) driver;
System.out.println(jse.executeScript("document.getElementById(condition).value");
String value = jse.executeScript("document.getElementById(condition).value").toString();
AssertVerify(value, "15");
Getting following error:
org.openqa.selenium.WebDriverException: JavaScript error (WARNING: The server did not provide any stacktrace information)
How to get use the javascript to get the value and assign to other variable.
Your code trial was near perfect. You need to pass the id
attribute of the element within single quotes i.e. '...'
as follows:
String value = ((JavascriptExecutor)driver).executeScript("document.getElementById('condition').value").toString();
As you are still unable to retrieve the value through executeScript()
method, you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
Use getAttribute()
to retrieve the text:
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("condition"))).getAttribute("value");
Use executeScript
to retrieve the text:
WebElement myElem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("condition")));
String value = ((JavascriptExecutor)driver).executeScript("document.getElementById('condition').value").toString();