Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to set style of a div element using JavascriptExecutor using C#. below is the code i tried but nothing happens

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;  
IWebElement element = driver.FindElement(By.XPath("//div[contains(@class,'rmSlide')]"));
js.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2])", 
element, "style", 
"visibility: visible;
height: 259px; 
width: 339px;
display: block;
overflow: hidden;
left: -81px;
top: 24px;
z-index: 2; "

All I am trying to do is setting the display: block from display: none

Below is the Html of the element

As you are trying to change the attribute display: none; to display: block; you can use the following code block :

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;  
IWebElement element = driver.FindElement(By.XPath("//div[contains(@class,'rmSlide')]"));
var script = "arguments[0].style.display='block';";
js.ExecuteScript(script, element);
                it should be display='block'?, i changed it and tried but no luck, it is not changing the display attribute
– Vincent
                Feb 2, 2018 at 11:17
                no error it did not change anything, Is it has to be do with the div property?, initially there is no style.  So i am performing click operation on the div and then the style is added after that i am performing this code.
– Vincent
                Feb 2, 2018 at 11:55
                Can you update the question with the raw and unedited but formatted and text-based HTML ?
– undetected Selenium
                Feb 2, 2018 at 11:56
                i have updated the question, let me explain what i am trying to do, MouseHover on Quick Links will load the li elements like dropdown.  But the drop down is very quick cannot perform any actions on it. so i am trying to block it with javascript.
– Vincent
                Feb 2, 2018 at 12:26
                If drop down is very quick still doesn't matters. Include the text-based formatted HTML of the DropDown element and text-based HTML of the <li> elements please.
– undetected Selenium
                Feb 2, 2018 at 12:30

I got the code right, just modified and added mouse hover on the element, below is the code.

var element = driver.FindElement(By.XPath("xpathe here"));
Actions action = new Actions(driver);
action.MoveToElement(element).Build().Perform();  
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;  
IWebElement element1 = driver.FindElement(By.XPath("//div[contains(@class,'rmSlide')]"));
js.ExecuteScript("arguments[0].setAttribute('style', 'visibility: visible; height: 259px; width: 339px; display: block; overflow: hidden; left: -81px; top: 24px; z - index: 2; ')", element1);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.