I have 'WebElement div' which represent my div:
<div style="width: 500px; height: 100px;">
I try to change height of my div like this:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement div = ...;
js.executeScript("document[0].style.height = '200px'", div);
but doesn't work.
And this is exception I get:
org.openqa.selenium.WebDriverException: unknown error: Cannot read property 'style' of undefined
How I can change height of my div?
I have 'WebElement div' which represent my div:
<div style="width: 500px; height: 100px;">
I try to change height of my div like this:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement div = ...;
js.executeScript("document[0].style.height = '200px'", div);
but doesn't work.
And this is exception I get:
org.openqa.selenium.WebDriverException: unknown error: Cannot read property 'style' of undefined
How I can change height of my div?
Share Improve this question edited May 8, 2018 at 12:28 KunLun asked May 8, 2018 at 12:15 KunLunKunLun 3,2444 gold badges29 silver badges91 bronze badges3 Answers
Reset to default 3document
wouldn't be a valid selector here. You would want to do something like document.querySelector("div").style.height = "200px"
You are passing div tag (WebElement
) as an argument to executeScript
method which will be passed on to JavaScript
function to be executed, as per this doc.
So, you need to use below code (arguments[0]
- first argument, instead of document[0]
):
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement div = ...;
js.executeScript("arguments[0].style.height = '200px'", div);
the javascript code is wrong here. It should be arguments[0]
instead of document[0]
.
Please change/replace the following line
js.executeScript("document[0].style.height = '200px'", div);
with the line
js.executeScript("arguments[0].style.height = '200px'", div);