简体   繁体   English

如何使用C#在Selenium中使用JavaScriptExecutor将样式属性设置为div

[英]How to set style property to div using JavaScriptExecutor in selenium using C#

I am trying to set style of a div element using JavascriptExecutor using C#. 我正在尝试使用C#使用JavascriptExecutor设置div元素的样式。 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 我想要做的就是设置display: blockdisplay: none display: block display: none

Below is the Html of the element 以下是元素的HTML

在此处输入图片说明

As you are trying to change the attribute display: none; 当您尝试更改属性display: none; to display: block; 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);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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