NIlesh Sharma
NIlesh Sharma

Reputation: 5655

How to set style display of an html element in a selenium test?

I get an element like

cv_upload = driver.find_element_by_id('id_cv_upload')

So I want to set its display as inline in python itself.is it possible with python to set the display. I tried

cv_upload.style.display = "inline"

which is showing me error.
One way which is getting into my mind is to use Jquery to change the display and then execute it using driver.execute but unfortunately I am not getting the right syntax to do this. Let me know how to do this (The syntax.) Thanks.

Upvotes: 21

Views: 91211

Answers (4)

Doni Tie
Doni Tie

Reputation: 31

In case your element doesn't have ID, you can do same using css

in python:

 driver.execute_script("document.querySelector('yourCSSLocatorGoesHere').style.display='block';")

in Java:

 WebDriver driver=new FirefoxDriver();   
    JavascriptExecutor js=(JavascriptExecutor) driver;
  driver.execute_script("document.querySelector('yourCSSLocatorGoesHere').style.display='block';")

Upvotes: 0

slva2000
slva2000

Reputation: 149

$('#box').css(
{
  background: "#FF0000",
  "box-shadow": "1px 1px 5px 5px red",
  width: "100px",
  height: "100px",
  display: "block"
}
);

BSI.id().setStyle() vs jQuery.css() vs. native DOM

Upvotes: 0

NIlesh Sharma
NIlesh Sharma

Reputation: 5655

Finally find out the way to set display of an element.

driver.execute_script("document.getElementById('id_cv_upload').style.display='block';")

basically using driver.execute_script I am executing a java script for setting the style of an element.

Upvotes: 4

wirey00
wirey00

Reputation: 33661

since you marked jQuery - You can use the css() function to set/get css attributes

$('#id_cv_upload').css('display','inline');

Upvotes: 47

Related Questions