简体   繁体   English

如何使用 Selenium 为元素设置样式属性?

[英]How to Set style property to element using Selenium?

driver.SwitchTo().Frame("contentFrame");
IWebElement str = driver.FindElement(By.XPath("//*[@id='dvCustomDateRange']"));

I need to change the style attribute from style="display: none;"我需要从 style="display: none;" 更改样式属性to style="display: block;".样式=“显示:块;”。

This is the element:这是元素:

<div id="dvCustomDateRange" tabindex="0" class="filters hidden inline-block inlineChilds NoPrint width100 marginBt7" style="display: block;">

any idea how to do this using Selenium?知道如何使用硒来做到这一点吗? Im try getAttribute and getCssValue methods to change value but without result.我尝试 getAttribute 和 getCssValue 方法来更改值但没有结果。

You will have to use javascript executor to set a css attribute value.您必须使用 javascript 执行程序来设置 css 属性值。

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.executeScript("arguments[0].style='display: block;'", element);

executeScript() 会做到这一点:

 driver.executeScript("arguments[0].style='display: block;'",element);

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

相关问题 如何在WPF中设置元素的样式属性 - How to set style property of an element in WPF 如何使用C#在Selenium中使用JavaScriptExecutor将样式属性设置为div - How to set style property to div using JavaScriptExecutor in selenium using C# 如何检查是否设置了样式属性? - How to check if a style property is set or not? 如何使用Style将子UserControl的Dependency属性绑定到宿主元素的视图模型的属性? - How to bind a child `UserControl`'s Dependency Property, using `Style`, to a property of the host element's view-model? 如何使用Selenium WebDriver,NUnit和C#获取元素属性的子属性值 - How to get child property value of a element property using selenium webdriver, NUnit and C# 如何使用selenium webdriver C#获取元素的style属性值 - How to get the value of style attribute of an element using selenium webdriver C# 如何通过样式设置器设置可为空的属性值? - How to set a nullable property value by Style setter? 如何在代码中设置ListBox控件的Style属性 - How to set the Style property of a ListBox control in code 如何通过样式触发器设置依赖项属性 - How to set a dependancy property from a style trigger 如何在WPF中为按钮样式设置Data属性? - how to set Data property for button style in wpf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM