-1

I have this funny bug happening to me today. I've been using Selenium for years already and never had an issue navigating to URL (via driver.navigate().to(url)) however today I'm attempting to navigate to a specific URL and I find that after executing the program several times it sometimes just stays on the original page without navigating to new page.

The funny thing is that this only happens about 50% of the time and it only happens when navigating to a specific URL at a specific part of the program (in other parts of program I have no issue navigating to this url).

Is it possible that some element on the current page is preventing driver.navigate().to(url) from executing?

I've looked at this and this question but both seem to have issues with navigating altogether. In my case, it sometimes works and sometimes doesn't (even when the exact same url is being used).

Also I'm not getting any specific errors (so I don't have much more info to post). The program just moves on as if the statement didn't exist.

I'll be happy to provide additional details if necessary.

Code:

shoppingCartURL.navToShoppingCart(driver);

String[] XPath = { "//*[contains (@value,'Delete')]" };

List<WebElement> elementList = webElementX.getElementListByXPath(driver, XPath);

System.out.println("Deleting " + elementList.size() + " element(s) from shopping cart");

for (int elementListCounter = 0; elementListCounter < elementList.size(); elementListCounter++) {

    WebElement singleElement = elementList.get(elementListCounter);

    try {
        singleElement.click();
    } catch (Exception e) {

        System.out.println("Exception occurred (StaleElementReferenceException)");
    }

}

if (conditionX == false) {
    productPage.navToProductPage(driver, product); // this method is not always executed, program continues to execute productPage.performActionOnPage(driver); without navigating to 'product page'
    productPage.performActionOnPage(driver);
}




public void navToProductPage(WebDriver driver, String product)

{
   String URL = "https://www.example.com/product/" + product;
   System.out.println("Navigating to " + URL); // always prints the  correct url (but still doesn't always navigate to url as mentioned in question)
   driver.navigate().to(URL);

}

Update:

I noticed ref=cart-ajax-error in redirect url (after deleting items from cart). Apparently, the site is using AJAX to refresh page after deleting items from cart. Might this conflict with my attempt to navigate to another page? In other words, perhaps Selenium is getting two different messages at the same time.. refresh page and navigate to new page.. so it remains on the current page?

If this is true, what can be done to resolve this issue?

Thanks!

S.O.S
  • 848
  • 10
  • 30
  • Paste your code in the description – Alok Apr 13 '18 at 01:39
  • Where are you declaring `product`? I don't it in the example. Is it possible that something is poisoning the value? Something that is not visible when you output it to the console, but is not valid as a URL so it fails to navigate. Just a thought. – MivaScott Apr 13 '18 at 02:12
  • did you checked driver.get(URL)? – santhosh kumar Apr 13 '18 at 06:19
  • MivaScott: No the url does not change after syso to navigation. santhosh kumar: driver.get() works fundamentally the same way as driver.navigate.to(url) I've updated question with additional details – S.O.S Apr 13 '18 at 15:33

1 Answers1

0

When it sometimes happens and sometimes not it's nearly always a matter of timing.

Add an explicit wait to your code after navigating to the URL:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.urlToBe(URL));
Frank
  • 831
  • 1
  • 11
  • 23
  • I tried this but it just leads to `selenium.TimeoutException: Expected condition failed` (since navigation failed, it will need to wait indefinitely) – S.O.S Apr 13 '18 at 15:34