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
-
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– Dan Snell2014年03月24日 17:19:15 +00:00Commented Mar 24, 2014 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– user29172392014年03月24日 18:29:55 +00:00Commented Mar 24, 2014 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 hereharaprasadj– haraprasadj2014年03月27日 06:46:03 +00:00Commented Mar 27, 2014 at 6:46
-
1are the explict waits firing correctly or timing out? Can you provide an segment of the html code for the page?Dan Snell– Dan Snell2014年03月27日 23:39:54 +00:00Commented Mar 27, 2014 at 23:39
-
Please see my comment here for focusing web element: stackoverflow.com/a/57221304/3181500user3181500– user31815002019年07月26日 13:43:29 +00:00Commented Jul 26, 2019 at 13:43
3 Answers 3
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.
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.
-
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 helpuser2917239– user29172392014年03月24日 18:28:39 +00:00Commented Mar 24, 2014 at 18:28
Try using Webdriver Advanced User Interaction to click the 'Save' button. I had sort of same issue and i resolved it using Advanced User Interactions.
You can find all the help you want regarding Advanced User Interactions in the link below. https://code.google.com/p/selenium/wiki/AdvancedUserInteractions
Let me know if it helps.
Explore related questions
See similar questions with these tags.