Deepika
Deepika

Reputation: 1

How to change the display property of the style attribute of an element using Selenium and Java

I am not able to change the style in div section using selenium Java. Please see picture I need to change the style of div which contain id is "navbar".

Snapshot of the HTML:

html

This is my code:

JavascriptExecutor js = null; 
  if (driver instanceof JavascriptExecutor) 
  { js
 = (JavascriptExecutor) driver; 
  }

WebElement element = driver.findElement(By.xpath("/html/body/div[1]"));
System.out.println(element);
//js.executeScript("arguments[0].setAttribute('style','display:none')", element);
js.executeScript("arguments[0].style.display = 'none'", element);

Upvotes: 0

Views: 1217

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

To set the display property of style attribute as block you can use:

((JavascriptExecutor) driver).executeScript("document.getElementById('navbar').style.display='block';");

Upvotes: 1

Related Questions