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

后端 未结 1 760
攒了一身酷
攒了一身酷 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 <body> 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 <body> 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 <iframe> before trying to find the <body> tag, switch back to the defaultContent as follows:

      driver.switchTo().defaultContent();
      

    Note: As per best practices always keep your Test Environment updated with the latest releases.

    • Update the Selenium Client dependency to 3.12.0:

      • selenium-java:

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.12.0</version>
        </dependency> 
        
      • selenium-server:

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.12.0</version>
        </dependency>
        
    0 讨论(0)
提交回复
热议问题