javascript - Change style height of div with JavaScriptExecutor Selenium Java - Stack Overflow

I have 'WebElement div' which represent my div:<div style="width: 500px; height: 100p

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 badges
Add a ment  | 

3 Answers 3

Reset to default 3

document 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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745310204a4621958.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信