Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Selenium elements to wait, check, click on without finding the elements again?

I am new to Selenium and was using Telerik free testing framework before. Problem is I am not able to understand, how to use elements which are already identified with [FindsBy] to wait, check and click on.

ex:

    [FindsBySequence]
    [FindsBy(How = How.Id, Using = "container-dimpanel")]
    [FindsBy(How = How.CssSelector , Using = ".btn.btn-primary.pull-right")]
    public IWebElement UpdateButton { get; set; }

    internal void ClickUpdateButton(TimeSpan timeout)
    {
        new WebDriverWait(_driver, timeout).
            Until(ExpectedConditions.ElementIsVisible(By.CssSelector(id));
        UpdateButton.Click();
    }

I want my code to wait for update button to be visible and then click on it. But I want to just pass the UpdateButton element rather than using By selector.

  • not sure if UpdateButton.Enabled will wait until its visible.
like image 714
Meg-90 Avatar asked Nov 09 '16 18:11

Meg-90


People also ask

What would you use if you are waiting for an element to be clickable in Selenium?

#1) elementToBeClickable() – The expected condition waits for an element to be clickable i.e. it should be present/displayed/visible on the screen as well as enabled. wait. until(ExpectedConditions. elementToBeClickable(By.

How do you wait for an element to appear in Selenium?

Explicit Wait in SeleniumBy using the Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code. Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load.

How would you click on an element which is not on the visible part of the screen?

We can click on an element which is hidden with Selenium webdriver. The hidden elements are the ones which are present in the DOM but not visible on the page. Mostly the hidden elements are defined by the CSS property style="display:none;".


2 Answers

There is an expected condition for visibility that accepts a WebElement: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#visibilityOf-org.openqa.selenium.WebElement-

Until also returns the element being waited for, so you can combine this into one line:

internal void ClickUpdateButton(TimeSpan timeout)
{
    WebDriverWait wait = new WebDriverWait(_driver, timeout);
    wait.Until(ExpectedConditions.visibilityOf(UpdateButton)).click();
}

However, in my frameworks I usually add a helper function that does this, as it get's used so much. You can also do similar things with wait until clickable, etc. and have methods that accept a WebElement or a By:

public WebElement waitThenClick(WebElement element) 
{
    WebDriverWait wait = new WebDriverWait(_driver, timeout);
    return wait.Until(ExpectedConditions.visibilityOf(UpdateButton)).click();
}
like image 183
nofacade Avatar answered Nov 14 '22 21:11

nofacade


The C# client doesn't have a builtin condition to check the visibility for a proxied WebElement.

Moreover the expected condition ExpectedConditions.ElementIsVisible checks that the element is displayed but doesn't check that the element is visible from a user perspective.

So the quickest and most reliable way is to retry the click in a waiter until success:

Click(UpdateButton, 5);
static void Click(IWebElement element, int timeout = 5) {
    var wait = new DefaultWait<IWebElement>(element);
    wait.IgnoreExceptionTypes(typeof(WebDriverException));
    wait.PollingInterval = TimeSpan.FromMilliseconds(10);
    wait.Timeout = TimeSpan.FromSeconds(timeout);
    wait.Until<bool>(drv => {
        element.Click();
        return true;
    });
}
like image 34
Florent B. Avatar answered Nov 14 '22 22:11

Florent B.