Max
Max

Reputation: 221

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;".

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.

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>

Upvotes: 0

Views: 1412

Answers (4)

shivansh
shivansh

Reputation: 26

I believe all you need to do is change your wait condition to ElementIsVisible

As long as the effective style of the element is 'display:none', the check would return 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();

Upvotes: 0

Dazed
Dazed

Reputation: 1551

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.

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

Upvotes: 0

Poloq
Poloq

Reputation: 663

You can try to check if element have "display: block;" in style. So selenium will wait until element will not change display to "block".

Css selector will be:

"#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 Y-B Cause.

Upvotes: 1

Zoette
Zoette

Reputation: 1281

This xpath will show you the buttons that don't have the attribute 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à'

Upvotes: 0

Related Questions