Selenium click link multiple times

I am automating tests for our webapp in Selenium WebDriver for C #. One of our test scenarios revealed the problem by clicking the save button multiple times, resulting in multiple identical entries.

The standard IWebElement.Click()

causes Selenium to block until the page is fully loaded. This means that by the time our second click comes to completion, a postback has taken place and we are no longer on the form page.

Does anyone know how to "manually" click an element that won't block Selenium?

+3


source to share


3 answers


If we use JavaScript to send clicks, Selenium will not be blocked and we can click multiple times. However, since our click causes the page to load, we cannot directly link to this element. Instead, we need to provide a location for the click and then fire our click events.

Since our WebApp uses JQuery, I was able to use the code shown here: How to simulate a click using x, y coordinates in JavaScript?

So, in the end, our C # logic looks something like this:



IWebElement element = driver.findElement(By.id("foobar"));
Point point = element.Location;
IJavascriptExecutor jscript = (IJavascriptExecutor)driver;
jscript.executeScript("$(document.elementFromPoint(arguments[0], arguments[0])).click();", point.X, point.Y);

      

Although this dispatches a click event, I'm not 100% sure the element is receiving it; I'll do some experiments and see.

0


source


You can either wait for a given amount of time for the page to load:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

      

... or be more dynamic and wait for your button to appear:

var driver = new WebDriver();
var wait = new WebDriverWait(driver, TimeSpan(0, 1, 0));
wait.Until(d => d.FindElement(By.Id("button"));

      

Source: fooobar.com/questions/36511 / ...

Selenium also has source code similar to the second method: http://selenium.googlecode.com/svn/trunk/dotnet/src/WebDriver.Support/UI/ExpectedConditions.cs



Let me know if this works for you. I personally use these options with WatiN :

browser.WaitForComplete();

      

... or:

browser.WaitUntilContainsText("Text");

      

It's a shame that Selene doesn't have the first.

+1


source


What you need to do is click through the javascript. In java it is done like this:

IJavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("gbqfd")));
executor.executeScript("arguments[0].click();", driver.findElement(By.id("gbqfd")));

      

Actually I think it looks a lot like this, it won't block selenium and you should be able to bind multiple before the page returns.

If this newer approach is too slow you can do it all faster in js for example.

IJavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById(id).click();");
executor.executeScript("document.getElementById(id).click();");

      

0


source







All Articles