Frankline
Frankline

Reputation: 40985

InvalidElementStateException while running Selenium Webdriver with PhantomJS

I'm running selenium tests which work OK in Firefox, but I get an error when using PhantomJS.

Here is my python code:

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

And here is the error I'm getting:

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

It is unable to find element id_name, yet when run with FireFox, works perfectly.

Anyone knows if there is a current bug with PhantomJS that addresses this issue?

Currently using Selenium 2.35.0 and PhantomJS 1.9.2 on Ubuntu 12.04

Upvotes: 12

Views: 7919

Answers (6)

Alexey Savchenko
Alexey Savchenko

Reputation: 11

I have fixed this problem with user-agent changing I set up to phantom js capabilities. Try this one.

Upvotes: 0

Rahul Satal
Rahul Satal

Reputation: 2237

I was getting the same issue with PhantomJS. I solved it by adding a line to set window size after creating a driver reference as-

driver = webdriver.PhantomJS()
driver.set_window_size(1124, 850) # set browser size.
driver.get("http:example.com") # Load page

The solution is we need to set a fake browser size before doing browser.get("").

Upvotes: 1

Mahesha
Mahesha

Reputation: 29

One way to get this right is to check if the it is displayed. field=driver.find_element_by_id("id_name") if field.is_displayed(): field.clear()

Upvotes: 0

Chris
Chris

Reputation: 37

I got the same trouble. I use selenium and PhantomJS in Python. I'm trying to sign in a webpage, when I use firefox, It always works,but PhantomJS doesn't even if I have "sleep" my code for 60s.The code as below:

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path='phantomjs.exe')
# driver = webdriver.Firefox()
driver.get("http://gs.swjtu.edu.cn/ws/gsedu")
time.sleep(60)
form_id = driver.find_element_by_name('userid')
form_id.send_keys('2015xxxxxx')
form_pswd = driver.find_element_by_name("userpwd")
form_pswd.send_keys('xxxxxx99201xxxxxx')
driver.find_element_by_class_name("loginbtn").submit()
print(driver.current_url)

I got the error as this:

selenium.common.exceptions.InvalidElementStateException: Message: {"errorMessage":"Element is not currently interactable and may not be manipulated"

But when I change a url, PhantomJS works.such as this:

driver = webdriver.PhantomJS(executable_path='phantomjs.exe')
driver.get("http://www.baidu.com")
driver.find_element_by_id('kw').send_keys('python')
driver.find_element_by_id('su').submit()
print(driver.current_url)

then the print is:

https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=python&rsv_pq=ceb307010003f0d6&rsv_t=3b72xijigkGFAWp%2FpwxdE0rDAVeegFlzHpa5aXwPD2mBVBRvSQbTKn7Ra%2FQ&rsv_enter=1&rsv_sug3=6&rsv_sug1=1&rsv_sug7=100&inputT=504&rsv_sug4=505

I guess the reason is that some web server deny the request form PhantomJS, accept request from normal clinet. some server not deny.

Upvotes: 1

Mikezx6r
Mikezx6r

Reputation: 16905

Another possibility is the field in question truly isn't interactable. PhantomJS isn't telling you it can't find the element, but that it can't interact with it. I had an issue like this (except it was the ChromeDriver), and all I had to do was make the browser window wider.

As promanski suggested, take a screenshot. If your layout is responsive, it's possible the PhantomJS window is narrower than the Firefox one, and causing your element to be rendered hidden. Or set the browser size wider for PhantomJS.

Worth a shot.

Upvotes: 2

promanski
promanski

Reputation: 567

I've used a PhantomJS, ChromeDriver and FirefoxDriver for functional testing. The difference between PhantomJS and others is that PhantomJS driver don't wait for page to be loaded and returns control to the test program (your python code). Please check this out!

I think this is not a correct behaviour of PhantomJS driver. I found in WebDriver's api documentation: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#get(java.lang.String) the following method of WebDriver which opens a given URL

void get(java.lang.String url)

The description is:

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

So it is possible that (and here I agree with @Ashley comment)

  • in Firefox a page is fully rendered before test code tries to access an element
  • in PhantomJS the test code tries to interact with a page earlier when not yet ready

You can find it easy if you "sleep" your test code for a, let's say, 2 seconds, just before you access first element in your test.

If this is it, you can try to resolve this issue by waiting for page to load (instead of 2 seconds) by checking some condition on your page, e.g. page title should be "My Form". Try it in loop until condition is true. Then run rest of your test.

Note: You can always take a screenshots, also in headless PhantomJS driver implementation! It will give you a possibility to debug a problem

Upvotes: 2

Related Questions