Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for input element enabled in selenium webdriver?

I am writing script to select and enter value in below screen in JIRA.

enter image description here The 'issue type' is an 'input' element with autocomplete property:

<input type="text" autocomplete="off" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" class="text aui-ss-field ajs-dirty-warning-exempt" id="issuetype-field" aria-controls="issuetype-suggestions">

Right after entering value for 'input type', the rest of fields such as 'summary' grey out for a few seconds. If I try to enter value for 'summary' field right after, using following code:

myDriver.findElement(By.id("summary")).sendKeys(summary);

summary field will not be entered, rather an error "Element not found in the cache - perhaps the page has changed since it was looked up.." will happen.

HTML code for 'summary' field is:

<input type="text" value="" name="summary" id="summary" class="text long-field">

So what i want is just to wait for 'summary' input field to be enabled and then sendkey to it. I am looking for something like

ExpectedConditions.presenceOfElementLocated 

but instead i want 'enabled', not presenceOfElementLocated or visibilityOfElementLocated. I do not want to use Thread.sleep() either.

Any suggestions?

like image 319
user1559625 Avatar asked Oct 06 '15 13:10

user1559625


People also ask

How do you wait until an element appears in Selenium?

Selenium: Waiting Until the Element Is Visible var wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.

How do you wait until an element is clickable on a website?

Wait until the element's . Displayed property is true (which is essentially what visibilityOfElementLocated is checking for). Wait until the element's . Enabled property is true (which is essentially what the elementToBeClickable is checking for).

What is the wait command in Selenium?

In automation testing, wait commands direct test execution to pause for a certain length of time before moving onto the next step. This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.


2 Answers

How about ExpectedConditions.elementToBeClickable(locator)?

elementToBeClickable checks if the element is enabled.

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#elementToBeClickable-org.openqa.selenium.By-

like image 176
vins Avatar answered Oct 06 '22 02:10

vins


public void waitForElementEnabled(final WebElement element) {
    try {
        getWait().until((ExpectedCondition<Boolean>) driver -> element.isEnabled());
    } catch (Exception e) {
        LOGGER.info(
                e + " : " + "Timed out waiting for element: " + element);
    }
}

The wait is a WebDriverWait and we want to wait until we get a true response when the WebElement is enabled. The driver is my WebDriver instance which is declared globally. If it timesout without getting a true response then I cantch the exception (so that the test will continue) and show a message informing that the operation timed out. I'm using a Lambda expression so you need to be on selenium 3.1 or higher (with guava 21)

like image 21
sideshowmanny Avatar answered Oct 06 '22 01:10

sideshowmanny