Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I have a Selenium WebDriver test where I enter some text into a text input box

    var input_Note = Driver.Instance.FindElement(By.Id("note"));
    input_Note.SendKeys("test");

I then attempt to click on the Save button, but it does not work. I was previously using Coded UI where there is a SetFocus element that points the focus towards whichever element you are targeting. Is there something similar in Selenium?

var button_Save = Driver.Instance.FindElement(By.Id("save"));
button_Save.Submit();

I think the problem is with the JavaScript on my page. My application can be slow and unpredictable. I tried telling it to wait until a certain field was not empty, but that didn't seem to work very well. Does anyone have any suggestions on how to handle this

share|improve this question
    
is there an action that occurs with the button once text has been entered? Are you getting any errors when the click action is performed? –  Dan Snell Mar 24 at 17:19
    
Hi Dan, yes. When the user enters text into that radio button, another text input gets auto-filled, which can sometimes take a second or two. I have tried using explicit waits, but no luck. Do you have any other suggestions? –  user2917239 Mar 24 at 18:29
    
Did you try click() instead of Submit()? UPDATE: Also you can try to wait for element to be clickable. Please find the expected conditions document here –  haraprasadj Mar 27 at 6:46
1  
are the explict waits firing correctly or timing out? Can you provide an segment of the html code for the page? –  Dan Snell Mar 27 at 23:39

2 Answers 2

You probably need to use a Selenium JavascriptExecutor and call the command to get focus using JavaScript. If your Firefox was in non-native selenium events mode, then this would make sense. I would try Selenium 2.40.0 with Firefox 27 though, with native events enabled, just to see what happens, otherwise use the JavascriptExecutor as a workaround. I have to use this method to get focus on new windows sometimes because Selenium wont do it.

share|improve this answer
    
Hi Djangofan, I am using the newest version of Firefox and Selenium WebDriver. Do you know a good resource to learn about using the JavascriptExecutor? Appreciate the help –  user2917239 Mar 24 at 18:28

I like to use a combination of techniques, the first involving a 'dependableClick()' which I have modified slightly starting with some code I found here on SE:

public void dependableClick(By by) throws InterruptedException
    {
        final int MAXIMUM_WAIT_TIME = 10;
        final int MAX_STALE_ELEMENT_RETRIES = 5;

        WebDriverWait wait = new WebDriverWait(driver, MAXIMUM_WAIT_TIME);
        int retries = 0;
        while (true) {
            try {
                wait.until(ExpectedConditions.elementToBeClickable(by)).click();
                return;
            } catch (StaleElementReferenceException e) {
                if (retries < MAX_STALE_ELEMENT_RETRIES) {
                    retries++;
                    continue;
                }
                else {
                    throw e;
                }
            } catch (WebDriverException e2) {
                System.out.println("dependableClick catches WebDriverException...");
                Thread.sleep(500);
                if (retries < MAX_STALE_ELEMENT_RETRIES) {
                    retries++;
                    continue;
                }
                else {
                    throw e2;
                }
            }
        }
    }

This helps anytime I need to click an element that, for any number of reasons, doesn't lend itself easily to being clicked. I then leverage that in another method specifically for entering text:

public void inputTextToElement(By by, String text) throws Exception {
    dependableClick(by);  // get focus on element
    driver.findElement(by).clear();
    driver.findElement(by).sendKeys(text);
}

In this method, the dependableClick() gets the focus after which we can continue as normal. An example call might look like this:

inputTextToElement(By.cssSelector(".inputbox_firstname"), "John"); 

With Chrome in particular, I find these sorts of 'brute force' helper methods to be invaluable for many otherwise simple tasks. When I start running into stale elements and missed clicks and such, I start looking into helper methods like these to encapsulate the error trapping that is sometimes necessary. Once these are working, they really help to get me back into the flow of writing automation without having to trip over the same nonsense again and again.

That said, I'm sure there are infinitely more elegant ways to approach this than the code I'm showing here, but this is working for me.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.