我正在运行在 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/