繁体   English   中英

通过具有和不具有css选择器的硒目标元素

[英]Selenium Target Element by having and not having a css selector

我要单击没有选择器“样式:无显示”的按钮。

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

目前,硒正在查找按钮本身,但它正在尝试单击,当然没有任何反应,因为它不可用。

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>

您可以尝试检查元素是否具有“ display:block;” 很有型。 因此,硒将等待,直到元素不会将显示更改为“块”。

CSS选择器将是:

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

编辑:

更好地使用:

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

如果样式完全没有显示,则此选择器将起作用。 或使用YB原因解决方案。

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

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

具有多种样式的按钮也可以使用,例如:

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

您可以使用以下方法,并按如下所示进行调用。 一旦正确显示了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;
            }
        });
    }

我相信您要做的就是将等待条件更改为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();

暂无
暂无

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

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