Random errors using wait for element clickable method in Selenium
This is what I use in a framework I've created. It eats ElementClickInterceptedException
and StaleElementReferenceException
and keeps trying until the timeout or success. It got rid of a lot of issues like what you are talking about. There are other ways to do it specific to each page but I found that this works really well in a large number of scenarios.
/// <summary>
/// Clicks on an element
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <param name="timeOut">[Optional] How long to wait for the element (in seconds). The default timeOut is 10s.</param>
public void Click(By locator, int timeOut = 10)
{
DateTime now = DateTime.Now;
while (DateTime.Now < now.AddSeconds(timeOut))
{
try
{
new WebDriverWait(Driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementToBeClickable(locator)).Click();
return;
}
catch (ElementClickInterceptedException)
{
// do nothing, loop again
}
catch (StaleElementReferenceException)
{
// do nothing, loop again
}
}
throw new Exception($"Unable to click element <{locator}> within {timeOut}s.");
}
Selenium not randomly clicking (No Error Code)
Your code waits for the elements to be clickable, but never actually clicks on the elements.
One possible solution:
clickMe= wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()])[%s]' % str(index + 1))))
clickMe.click()
Related Topics
How to Get the Available Wifi Aps and Their Signal Strength in .Net
How to Solve Disturbance in My Bot in C#
Nsubstitute - Testing for a Specific Linq Expression
How to Get a Type's Alias Through Reflection
How to Automate Sap Gui with C#
Xunit.Net: Global Setup + Teardown
Rounding Integers to Nearest Multiple of 10
Print List of Objects to Console
What to Use: Var or Object Name Type
How to Get the Development/Staging/Production Hosting Environment in Configureservices
Linq to Entities Does Not Recognize the Method 'System.Web.Mvc.Fileresult'
How to Read and Write Id3 Tags to an Mp3 in C#
How to Concatenate Two Ienumerable<T> into a New Ienumerable<T>
When Is Readerwriterlockslim Better Than a Simple Lock
How to Make a Combobox Non-Editable in .Net