WebDriverException:在Mac OS X上将Safari 11与WebDriver一起使用时,无法将返回值转换为WebElement:{} [英] WebDriverException: Returned value cannot be converted to WebElement: {} while using WebDriver with Safari 11 on Mac OS X

查看:274
本文介绍了WebDriverException:在Mac OS X上将Safari 11与WebDriver一起使用时,无法将返回值转换为WebElement:{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个硒Webdriver脚本,该脚本对我的被测应用程序执行了一些回归测试.该脚本可以在Google Chrome,Firefox,IE等上完美运行.

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.

但是,最近,我尝试在Mac OS X(10.13.5)的Safari(11.1.1)上运行它,即使我调用

However, recently I tried running it on Safari (11.1.1) on Mac OS X (10.13.5), and my script fails with a weird message even when I call a simple line such as

driver.findElement(By.tagName("body"));

我得到的异常如下:

org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {}
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'Yethis-MacBook-Pro.local', ip: '192.168.2.197', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.5', java.version: '1.8.0_171'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:375)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByTagName(RemoteWebDriver.java:441)
    at org.openqa.selenium.By$ByTagName.findElement(By.java:334)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
    at org.ycs.selenium.safari.App.execute(App.java:75)
    at org.ycs.selenium.safari.App.main(App.java:27)
Caused by: java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to org.openqa.selenium.WebElement
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:373)
    ... 5 more

硒版本:3.0.1 Safari版本:11.1.1 Java版本:1.8 OS X版本:10.13.5

Selenium Version: 3.0.1 Safari Version: 11.1.1 Java Version: 1.8 OS X version: 10.13.5

这是我在maven中配置的依赖项

This is my dependency configured in maven

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>3.0.1</version>
</dependency>

我尝试过大量搜索以解决此问题,但不幸的是,有关Mac的特定信息没有发现任何问题.

I tried doing a lot of search for this issue, but unfortunately, nothing showed up with Mac specific information.

有人可以帮我解决这个问题吗?

Could anyone help me solve this problem please?

谢谢, 斯里拉姆·斯里达拉恩(Sriram Sridharan)

Thanks, Sriram Sridharan

推荐答案

此错误消息...

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

  • 这篇关于WebDriverException:在Mac OS X上将Safari 11与WebDriver一起使用时,无法将返回值转换为WebElement:{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆