0

I am trying to click an accept terms checkbox while signing up for an account in selenium. My code seems to work, but it only works on some of my customers computers. On those who it doesn't work, it throws a staleelementexception. I have looked online to find a way to handle it such as

    
            bool result = false;
            int attempts = 0;
            Thread.Sleep(500);
            
            while (attempts < 100)
            {
                Thread.Sleep(700);
                try
                {

                    driver.Navigate().Refresh();
                    IJavaScriptExecutor javascriptExecutor = (IJavaScriptExecutor)driver;
                    Actions action = new Actions(driver);
                    IWebElement db = driver.FindElement(By.XPath("/html/body/div[1]/div/div[2]/form/div[1]/label/span[2]"));
                    action.MoveToElement(db).Build().Perform();
                    javascriptExecutor.ExecuteScript("arguments[0].click();", db);
                    result = true;
                    
                    break;
                }
                catch (WebDriverException ee)
                {}
                attempts++;
            }

            nextPage(driver, wait);

I have also tried just clicking on it with the driver and clicking with actions, but nothing seems to be working for certain users. I am at the point of not knowing the solution or not knowing what else I can do.

UPDATE 1: While I mentioned this in the comments, I still do receive a stale element, but I seem to of found the cause. When using specific country proxies, it seems that this is thrown. Now the element is still there, but why would the proxies cause this to happen?

Darth Gonk
  • 45
  • 6

1 Answers1

1

A stale element usually means the DOM changed from the moment you found the element. There are a few things you can try:

  • Inspect the page and try to determine what is changing for those users. Maybe MoveToElement triggers a tooltip or some classes to change. If you can identify such change you can wait for it to be done and then get the element
  • Try using fluent waits as explained here
  • Try clicking the element as soon as it is identified driver.findElement(by.xpath(xpath)).click();

Note: The JavaScript click that you are using in the snippet works well for CLickIntercepted exceptions, not so much for StaleElements. The element will be stale regardless on how you click it

Ioana Nicu
  • 36
  • 5
  • Thank you for your reply. Unforuantly, I have already tried all these. I have tried `MoveToElement` with `Actions` and `JavaScriptExecutor`. Neither worked, but I found that the issue revolves around the country the users proxies were based out of. – Darth Gonk Oct 01 '21 at 10:16
  • If you found the root cause consider writing your own answer to help the next guy stumbling on a similar issue. – Ioana Nicu Oct 04 '21 at 11:15
  • This issue is though, I feel the proxies should not be the cause of the issue. The element still exist, but for some reason it throws the stale element. – Darth Gonk Oct 04 '21 at 21:01