简体   繁体   中英

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

要将样式属性的display属性设置为block您可以使用:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM