【问题标题】:How to Set style property to element using Selenium?如何使用 Selenium 将样式属性设置为元素?
【发布时间】:2020-06-22 21:19:15
【问题描述】:
driver.SwitchTo().Frame("contentFrame");
IWebElement str = driver.FindElement(By.XPath("//*[@id='dvCustomDateRange']"));

我需要从 style="display: none;" 更改样式属性到样式=“显示:块;”。

这是元素:

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

知道如何使用 Selenium 做到这一点吗?我尝试使用 getAttribute 和 getCssValue 方法来更改值但没有结果。

【问题讨论】:

  • 但是在您提供的 HTML 中,它已经显示为 style="display: block;"。为什么??
  • 这是因为我在页面源代码中手动单击。
  • 那么意味着你什么时候在自动化过程中点击变得不可见??
  • 当我将样式更改为无时,它变得不可见。

标签: c# selenium selenium-webdriver


【解决方案1】:

您必须使用 javascript 执行器来设置 css 属性值。

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

【讨论】:

    【解决方案2】:

    executeScript() 可以解决问题:

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

    【讨论】:

      猜你喜欢
      • 2015-02-21
      • 2018-07-12
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 2018-01-12
      相关资源
      最近更新 更多