最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论