且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WebDriverException:在Mac OS X上将Safari 11与WebDriver一起使用时,无法将返回值转换为WebElement:{}

更新时间:2023-01-13 11:05:44

此错误消息...

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

...表示在 JVM 尝试将返回的值转换为 WebElement 时引发了WebDriverException.

...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 ,表明代码已尝试将对象强制转换为不是实例的子类.例如,以下代码生成ClassCastException:


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);


出了什么问题

不清楚您的用例为何需要获取<body>标记.但是按照以下讨论:


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:

  • 当页面仍在加载时,也许当某些 JavaScript / Ajax 仍处于活动状态时,您的脚本/程序试图访问<body>标记.
  • 解决方案:为要与之交互的 WebElement 生成 WebDriverWait ,如下所示:

  • 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")));

  • 如果要获取页面源,请使用 getPageSource() 方法,如下所示:

  • If you want to get the Page Source use getPageSource() method as follows:

    System.out.println(driver.getPageSource());
    

  • 如果在尝试找到<body>标记之前该程序的控件位于<iframe>之内,请切换回 defaultContent ,如下所示:

  • 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.

    • Selenium Client 依赖项更新为 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>