-1

I wanted to make use of WebDriverWait however I got this exception: "java.lang.ClassCastException: com.sun.proxy.$Proxy19 cannot be cast to org.openqa.selenium.By"

It is caused by the 3rd line of code -by why?? In another class I have all the locators and wanted to make a reference to it.

WebDriverWait wait = new WebDriverWait(SeleniumDriver.getDriver(), 10);
galleryPageLocators.galleryBlock.click();
wait.until(ExpectedConditions.visibilityOfElementLocated((By) galleryPageLocators.addToCartBtn));
galleryPageLocators.addToCartBtn.click();
0

2 Answers 2

1
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/someurl");
WebElement dynamicElement = (new WebDriverWait(driver, 10))
    .until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicElement")));

This is the format for webdriver wait

In your code you are down casting a webelement to By which is not allowed

If you need to use webelement instead of By , then use visibilityOf(webelement)

driver.get("http://somedomain/someurl");
    WebElement dynamicElement = (new WebDriverWait(driver, 10))
        .until(ExpectedConditions.visibilityOf(galleryPageLocators.addToCartBtn));
0

From what I can see in your question, you have very basic knowledge of Java so that I would recommend to start from learning the concepts of the language. In your particular case you're trying to cast some object to By class that has no parent-child relationships with By class. This is why you get class cast exception.

I also assume that you have done that because this was what your IDE suggested you yo do because it failed to find the method with appropriate parameter set. However this is not always a good advice. The better way is to examine what could be the other methods applicable in your case. For example the good solution is suggested by @PDHide.

The last thing I can assume is that your are using a page object that was initiated with PageFactory.init(). While the approach of awaiting elements with Wait-like classes is still valid, personally I prefer another way that is specially supported by Selenium to be used in with PageObject approach. The latter one is to instantiate your pages not with just default init(..) but with your custom extension of AjaxElementLocator where you override isElementUsable method. Some details about such approach you can find here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.