python - 使用 PhantomJS 运行 Selenium Webdriver 时出现 InvalidElementStateException

标签 python selenium selenium-webdriver phantomjs

我正在运行在 Firefox 中运行正常的 selenium 测试,但在使用 PhantomJS 时出现错误。

这是我的python代码:

    driver.find_element_by_link_text("Add Province").click()
    driver.find_element_by_id("id_name").clear()
    driver.find_element_by_id("id_name").send_keys("Frosinone")
    driver.find_element_by_id("id_code").clear()
    driver.find_element_by_id("id_code").send_keys("FR")

这是我得到的错误:

driver.find_element_by_id("id_name").clear()
self._execute(Command.CLEAR_ELEMENT)
return self._parent.execute(command, params)
self.error_handler.check_response(response)
raise exception_class(message, screen, stacktrace)
E       InvalidElementStateException: Message: u'Error Message => \'Element is not currently interactable and may not be manipulated\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:38159","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"sessionId\\": \\"e0d4d1b0-2f36-11e3-af69-b579903d9fbd\\", \\"id\\": \\":wdc:1381139859399\\"}","url":"/clear","urlParsed":{"anchor":"","query":"","file":"clear","directory":"/","path":"/clear","relative":"/clear","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/clear","queryKey":{},"chunks":["clear"]},"urlOriginal":"/session/e0d4d1b0-2f36-11e3-af69-b579903d9fbd/element/%3Awdc%3A1381139859399/clear"}' ; Screenshot: available via screen

它无法找到元素 id_name,但在使用 FireFox 运行时,运行完美。

有人知道 PhantomJS 当前是否存在解决此问题的错误吗?

当前在 Ubuntu 12.04 上使用 Selenium 2.35.0 和 PhantomJS 1.9.2

最佳答案

我使用 PhantomJS、ChromeDriver 和 FirefoxDriver 进行功能测试。 PhantomJS 与其他驱动程序的区别在于 PhantomJS 驱动程序不等待页面加载并将控制权返回给测试程序(您的 python 代码)。请检查一下!

我认为这不是 PhantomJS 驱动程序的正确行为。我在 WebDriver 的 api 文档中找到: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#get(java.lang.String)打开给定 URL 的以下 WebDriver 方法

void get(java.lang.String url)

描述是:

Load a new web page in the current browser window. This is done (...) and the method will block until the load is complete.

所以有可能(在这里我同意@Ashley 的评论)

  • 在 Firefox 中,页面在测试代码尝试访问元素之前完全呈现
  • 在 PhantomJS 中,测试代码尝试在尚未准备好时更早地与页面交互

如果在访问测试中的第一个元素之前让测试代码“休眠”(比方说,2 秒),您会发现这很容易。

如果是这样,您可以尝试通过检查页面上的某些条件等待页面加载(而不是 2 秒)来解决此问题,例如页面标题应为“我的表格”。循环尝试直到条件为真。然后运行剩下的测试。

注意:您始终可以截取屏幕截图,在 headless PhantomJS 驱动程序实现中也是如此!它会给你调试问题的可能性

关于python - 使用 PhantomJS 运行 Selenium Webdriver 时出现 InvalidElementStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19222631/

相关文章:

python - SciPy 累积分布函数绘图

python - 类型错误:不支持的操作数类型/: 'str' 和 'str'

selenium-webdriver - 如何从 WebDriver 的本地缓存中保存图像?

python - 如何使用Python + Selenium从网站下载特定文件

selenium - 如何使用 Selenium WebDriver 截取屏幕截图?

java - 我怎样才能得到这个值?

java - 脚本应该等到浏览器手动关闭

python - Matplotlib 无法在 macOS 中运行。显示错误。我尝试重新安装所有内容,但仍然无法正常工作

python - 无法在后端使用 python flask 和 ssl 验证 Facebook webhook 订阅中的 URL

javascript - 如何按特定顺序运行 Nightwatch 测试?