javaseleniumselenium-webdriverwebdriverwaitexpected-condition

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status error while clicking using Selenium


I am using Selenium with Java in Eclipse and I am getting these error very often before I was getting error due to loading status now another.

Scenario: after opening the Website page there are several click that are supposed to happen and every time it is stuck on different stages

Traceback:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status
from unknown error: unexpected command response
  (Session info: chrome=103.0.5060.114)
Build info: version: '4.3.0', revision: 'a4995e2c09*'
System info: host: 'AWAIS-PC', ip: '192.168.1.62', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '18.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [379a531d077c5128f9ab7816b39e5bdd, clickElement {id=6b67a9d0-7b57-4279-a7e4-b1e917a94925}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 103.0.5060.114, chrome: {chromedriverVersion: 103.0.5060.53 (a1711811edd7..., userDataDir: C:\Users\WRP\AppData\Local\...}, goog:chromeOptions: {debuggerAddress: localhost:61026}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:61026/devtoo..., se:cdpVersion: 103.0.5060.114, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Element: [[ChromeDriver: chrome on WINDOWS (379a531d077c5128f9ab7816b39e5bdd)] -> css selector: button[class='CartTotal-secureCheckout'] span]
Session ID: 379a531d077c5128f9ab7816b39e5bdd
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:257)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:78)
    at First_Test.Practice_First.main(Practice_First.java:125)

Code:

WebDriver driver = new ChromeDriver();
WebDriverWait waits = new WebDriverWait(driver, Duration.ofSeconds(10));
//driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//Thread.sleep(3000);
driver.manage().window().maximize();
//Thread.sleep(3000);
//  String originalWindow=  driver.getWindowHandle();
// Thread.sleep(5000);
waits.until(ExpectedConditions.visibilityOfElementLocated(By.id("wzrk-cancel")));
//waits.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#wzrk-cancel")));
driver.findElement(By.id("wzrk-cancel")).click();

// driver.findElement(By.xpath("//button[@id='wzrk-cancel']")).click();
waits.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href*='/men']")));
driver.findElement(By.cssSelector("a[href*='/men']")).click();

JavascriptExecutor Js1 = (JavascriptExecutor) driver;
Js1.executeScript("window.scrollBy(0,2500)");

waits.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[normalize-space()='Hibiscus Camo Arch Logo T-shirt in Cotton']")));
Thread.sleep(1000);
driver.findElement(By.xpath("//div[normalize-space()='Hibiscus Camo Arch Logo T-shirt in Cotton']")).click();

waits.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[title='Blue']")));
driver.findElement(By.cssSelector("button[title='Blue']")).click();

//waits.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@title='Blue']")));
// driver.findElement(By.xpath("//button[@title='Blue']")).click();
driver.findElement(By.xpath("//div[@class='Select-control']")).click();
driver.findElement(By.xpath("//*[text()='XL']")).click();
Thread.sleep(1000);
driver.findElement(By.cssSelector("[data-cy^='addToBagButton']")).click();
Thread.sleep(2000);
waits.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[class='Select-control']")));
driver.findElement(By.xpath("//div[@class='Select-control']")).click();
driver.findElement(By.xpath("//*[text()='L']")).click();
driver.findElement(By.cssSelector("[data-cy^='addToBagButton']")).click();
Thread.sleep(1000);
// waits.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[class^='Select-control']")));

waits.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[normalize-space()='GO TO BAG']")));
Thread.sleep(1000);
driver.findElement(By.xpath("//a[normalize-space()='GO TO BAG']")).click();

// driver.findElement(By.xpath("//span[normalize-space()='Secure Checkout']")).click();
waits.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[class='CartTotal-secureCheckout'] span")));
driver.findElement(By.cssSelector("button[class='CartTotal-secureCheckout'] span")).click();
//  Thread.sleep(2000);
waits.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".Map-enterAddressManuallyButton")));
driver.findElement(By.cssSelector(".Map-enterAddressManuallyButton")).click();

driver.findElement(By.xpath("//label[@for='regionId']//div[@class='Select Dropdown has-value is-searchable Select--single']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[text()='Ajman']")).click();

Solution

  • As @ArundeepChohan mentioned in his comments ideally to click on any clickable element instead of visibilityOfElementLocated() you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategy:

    WebDriverWait waits = new WebDriverWait(driver, Duration.ofSeconds(10));
    waits.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[class='CartTotal-secureCheckout'] span")));
    driver.findElement(By.cssSelector("button[class='CartTotal-secureCheckout'] span")).click();
    

    In a single line:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[class='CartTotal-secureCheckout'] span"))).click();