WebDriverException: Returned value cannot be converted to WebElement: {} while using WebDriver with Safari 11 on Mac OS X

后端 未结 1 768
攒了一身酷
攒了一身酷 2020-12-22 01:53

I have a selenium webdriver script which performs some regression tests on my application under test. The script works perfectly on Google Chrome, Firefox, IE, etc.

1条回答
  •  感情败类
    2020-12-22 02:32

    This error message...

    org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {}
    

    ...implies that WebDriverException was raised while JVM tried to cast the returned value into a WebElement.

    However your main issue is as follows:

    java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to org.openqa.selenium.WebElement
    

    ClassCastException

    ClassCastException is thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. As an example, the following code generates a ClassCastException:

    Object x = new Integer(0);
    System.out.println((String)x);
    

    What went wrong

    It is not clear about your usecase why you require to grab the tag. But as per the following discussions:

    • java.lang.ClassCastException exception while clicking/inputing on any web object
    • WebDriver and Firefox 4+: this.getWindow() is null

    There can be three possibilities of this error as follows:

    • Your script/program was trying to access the tag when the page was still loading perhaps when some JavaScript / Ajax was still active.
    • Solution: Induce WebDriverWait for the WebElement to whom you desire to interact with as follows:

      WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("elementID")));
      
    • If you want to get the Page Source use getPageSource() method as follows:

      System.out.println(driver.getPageSource());
      
    • If the control of the program was within an