Reputation: 121
I am still new to Selenium test world, and I am currently using the selenium chrome webdriver for testing.
what I want to do is in the below popup dialog:
/*
....
a bunch of assert and wait.until function to make sure everything is correct till this step.
....
*/
driver.findElement(By.xpath(DIALOGBOX + "/input")).sendKeys("10");
//Thread.sleep(500);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(APPLY_BUTTON)));
new Actions(driver).moveToElement(driver.findElement(By.xpath(APPLY_BUTTON))).click().perform();
and here is what I discovered till now, If I uncomment Thread.sleep(500)
, everything will work perfectly. but without Thread.sleep(500)
, the web application will not make a post call to the server which the Apply button should trigger.
I do not know why it happens, what Thread.sleep(500)
made difference to click action, is selenium takes the time to move to the element and performs the click with the mouse not yet reach the button position? and how to fix my cases without using thread sleep?
first, I forget to mention that the web application is built on GWT(Google web toolkit)
. I have already make sure the driver has successfully got the button element before click(), so the bug is minimized to click() of Action.
then I tried
new Actions(driver).moveToElement(driver.findElement(By.xpath(APPLY_BUTTON)).sendKeys(Keys.ENTER).perform();
it works perfectly, no need to make the thread sleep. so the cause of the bug is that selenium's click action is different from how GWT handle the onclick function. If you face the same issue, try using sendKeys and for the client side adding handler for onKeyPress
Upvotes: 0
Views: 7587
Reputation: 121
first, I forget to mention that the web application is built on GWT(Google web toolkit). I have already make sure the driver has successfully got the button element before click(), so the bug is minimized to click() of Action. then I tried
new Actions(driver).moveToElement(driver.findElement(By.xpath(APPLY_BUTTON))
.sendKeys(Keys.ENTER).perform();
it works perfectly, no need to make the thread sleep. so the cause of the bug is that selenium's click action is different from how GWT handle the onclick function. If you face the same issue, try using sendKeys and for the client side adding handler for onKeyPress
Upvotes: 0
Reputation: 3123
Just because the button is visible and selenium thinks it is clickable doesn't mean it is ready to use. Check with browser inspector and investigate if it is tied to some asynchronous javascript. Meaning, the alert may be waiting on some background process to complete before you can actually click it for an event to occur.
It's possible that upon entering a value into the input field, there is an async script going out to a server, and causing the "Apply" button to do nothing when clicked, until there is a response from the server. And that 500ms delay is just enough time for the ajax communication to complete.
To learn more about ajax: https://www.tutorialspoint.com/ajax/what_is_ajax.htm
Upvotes: 1
Reputation: 512
I will probably try the js.ExecuteScript method as ( below code is for c#)
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string jsOutput = (string)js.ExecuteScript(String.Format("document.getElementById('{0}').click();", elementId));
where the normal click
fails
Upvotes: 0