【问题标题】:Selenium Target Element by having and not having a css selectorSelenium 目标元素通过具有和不具有 css 选择器
【发布时间】:2016-12-01 08:00:44
【问题描述】:

我想在按钮没有选择器“style: display-none”时点击它

<div style="display: none;" id="pdp_size_select_container" class="select_size float_left" title="Select a Size">
</div>

现在,selenium 正在寻找 按钮 本身,但它正在尝试点击,当然没有任何反应,因为它不可用。

new WebDriverWait(driver, TimeSpan.FromMinutes(10)).Until(ExpectedConditions.ElementExists((By.Id("pdp_size_select_container"))));
        IWebElement sizeselect = driver.FindElement(By.Id("pdp_size_select_container"));
        sizeselect.Click();

我想要一种方法来搜索 ID,而没有选择器 style="display: none;" 的元素。

如果您感到困惑,网页上有一个隐藏按钮。在特定时间,您可以点击它。但我当时正在循环检查,当按钮的样式选择器消失时,我想用 WEBDRIVERWAIT 循环检查按钮。

这是实际可用时的代码,上面是不可用时的代码。

<div style="display: none;" id="pdp_size_select_container" class="select_size float_left" title="Select a Size">
</div>

【问题讨论】:

    标签: c# selenium firefox


    【解决方案1】:

    您可以尝试检查元素是否有“显示:块;”很有型。 所以硒会等到元素不会将显示更改为“块”。

    Css 选择器将是:

    "#pdp_size_select_container[style*='display: block;']"
    

    编辑:

    更好的使用:

    "#pdp_size_select_container:not([style*='display: none;'])"
    

    如果样式根本没有显示,此选择器将起作用。或者使用 Y-B Cause 的解决方案。

    【讨论】:

    • 所以你的意思是即使“inspect element”中的元素不显示display:block,它默认为display block,我可以查找?
    • 否,但如果他们在这种情况下没有样式属性,您可以使用“#pdp_size_select_container:not([style])”
    • 是的,但这很脆弱:元素可能有一天会有样式(颜色:红色或与其显示无关的任何颜色)
    【解决方案2】:

    此 xpath 将向您显示不具有属性 style='display: none' 的按钮。

    //div[not(contains(@style, 'display: none'))][id='pdp_size_select_container']
    

    它也适用于具有多种样式的按钮,例如:

    style='display: none; foo: bar; ceci: celà'
    

    【讨论】:

    • 你能用 (By.?("") I ​​am a little new
    • 当然。这是:By.xpath("//div[not(contains(@style, 'display: none'))][id='pdp_size_select_container']")
    【解决方案3】:

    您可以使用下面的方法并像下面这样调用它。然后在正确显示 xpath 后添加您希望它执行的下一步操作。

             WaitForElementToNotExist("//myxpath']",20, SeleniumDriver);
    
    
             public static void WaitForElementToNotExist(string xpath, int seconds, IWebDriver driver)
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
            wait.Until<bool>((d) =>
            {
                try
                {
                    // If the find succeeds, the element exists, and
                    // we want the element to *not* exist, so we want
                    // to return true when the find throws an exception.
                    IWebElement element = d.FindElement(By.XPath(xpath));
                    return false;
                }
                catch (NoSuchElementException)
                {
                    return true;
                }
            });
        }
    

    【讨论】:

      【解决方案4】:

      我相信您需要做的就是将等待条件更改为 ElementIsVisible

      只要元素的有效样式是'display:none',检查就会返回false。

      new WebDriverWait(driver, TimeSpan.FromMinutes(10)).Until(ExpectedConditions.ElementIsVisible((By.Id("pdp_size_select_container"))));
          IWebElement sizeselect = driver.FindElement(By.Id("pdp_size_select_container"));
          sizeselect.Click();
      

      【讨论】:

        猜你喜欢
        • 2015-10-15
        • 2023-03-09
        • 2014-08-04
        • 1970-01-01
        • 1970-01-01
        • 2015-09-15
        • 1970-01-01
        • 1970-01-01
        • 2011-05-12
        相关资源
        最近更新 更多