简体   繁体   English

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

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

I want to click a button as soon as it does not have the selector "style: display-none" 我要单击没有选择器“样式:无显示”的按钮。

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

Right now, selenium is finding the button itself, but it is trying to click and of course nothing happens because it is not available. 目前,硒正在查找按钮本身,但它正在尝试单击,当然没有任何反应,因为它不可用。

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

I want a way to search for an element that has the ID, and doesn't have the selector style="display: none;". 我想要一种搜索具有 ID且没有选择器style =“ display:none;”的元素的方法。

If you are confused, there is a hidden button on the web page. 如果您感到困惑,网页上会有一个隐藏的按钮。 At a certain time, it will be available for you to click. 在特定时间,您可以单击它。 But I am loop checking for that time, and I want to loop check with WEBDRIVERWAIT for the button when its style selector disappears. 但是我当时正在循环检查,当按钮的样式选择器消失时,我想用WEBDRIVERWAIT对该按钮进行循环检查。

This is the code when it is actually available, wheras above it was the code when it was unavailable. 这是实际可用时的代码,而上方是不可用时的代码。

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

You can try to check if element have "display: block;" 您可以尝试检查元素是否具有“ display:block;” in style. 很有型。 So selenium will wait until element will not change display to "block". 因此,硒将等待,直到元素不会将显示更改为“块”。

Css selector will be: CSS选择器将是:

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

Edit: 编辑:

Better use: 更好地使用:

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

This selector will work if style have not display at all. 如果样式完全没有显示,则此选择器将起作用。 Or use solution from YB Cause. 或使用YB原因解决方案。

This xpath will show you the buttons that don't have the attribute style='display: none'. 该xpath将向您显示不具有属性style ='display:none'的按钮。

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

It will also work will button having several styles, like: 具有多种样式的按钮也可以使用,例如:

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

You could use the below method and call it like the below. 您可以使用以下方法,并按如下所示进行调用。 Then add your next step for what you want it to do once that xpath is displayed correctly. 一旦正确显示了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;
            }
        });
    }

I believe all you need to do is change your wait condition to ElementIsVisible 我相信您要做的就是将等待条件更改为ElementIsVisible

As long as the effective style of the element is 'display:none', the check would return false. 只要该元素的有效样式为'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