So I've been having trouble with clicking a checkbox using the correct xpath. It only seems to work when the checkbox is visible after scrolling down the page. I came across some javascript code called scrollviewandclick that is used in conjunction with selenium. So, whenever I want to click something that is out of view, I simply use:
objCommon.ScrollInToViewAndClick(driver.FindElement(By.Xpath("YOUR Locator")));
I have also tried clicking by moving my mouse to the checkbox with the following code:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(driver.FindElement(By.Xpath("YOUR Locator"))).Click();
However, I am struggling to combine the scrollviewandclick method with this click action as it does not allow me to put two waits together. Can anyone provide insight into where I might be going wrong?
Below is the code for the scrollviewandclick method:
public void ScrollInToViewAndClick(IWebElement element)
{
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].scrollIntoView(true);", element);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
var elementList = new List<IWebElement>
{
element
};
var readonlyCollection = new ReadOnlyCollection<IWebElement>(elementList);
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.VisibilityOfAllElementsLocatedBy(readonlyCollection));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(element)).Click();
}