V
V
Viktor2016-04-21 14:11:14
Python
Viktor, 2016-04-21 14:11:14

Send_keys doesn't work when using PhantomJs, what am I doing wrong?

I'm using Selenium 2.53 and PhantomJs 2.1.1
The script stops all the time when you need to enter text when using Firefox () - everything works fine, but with PhantomJs it gives an error. Tell me what could be the reason?


Traceback (most recent call last):
File "D:/Code/Python/Scripts/HowToPhantomJs.py", line 19, in
element = driver.find_element_by_id('lst-ib')
File "C:\Python33\lib\site -packages\selenium\webdriver\remote\webdriver.py", line 266, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python33\lib\site-packages\selenium\webdriver \remote\webdriver.py", line 744, in find_element
{'using': by, 'value': value})['value']
File "C:\Python33\lib\site-packages\selenium\webdriver\remote \webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "C:\Python33\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with id 'lst-ib'","request":{"headers":{"Accept":"application/json"," Accept-Encoding":"identity","Connection":"close","Content-Length":"87","Content-Type":"application/json;charset=UTF-8","Host":" 127.0.0.1:2065","User-Agent":"Python-urllib/3.3"},"httpVersion":"1.1","method":"POST","post":"{\"value\": \"lst-ib\", \"sessionId\": \"dcf83270-082e-11e6-b6bb-df390d6d93bb\", \"using\": \"id\"}","url":"/element" ,"urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element"," port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source ":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/dcf83270-082e-11e6-b6bb-df390d6d93bb/element"}}"port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":""," source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/dcf83270-082e-11e6-b6bb-df390d6d93bb/element"}}"port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":""," source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/dcf83270-082e-11e6-b6bb-df390d6d93bb/element"}}
Screenshot: available via screen

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0")


phantomjs_path = r'C:\Users\Viktor\AppData\Roaming\npm\node_modules\phantomjs-1.9.8\bin\phantomjs.exe'
driver = webdriver.PhantomJS(phantomjs_path)
driver.set_window_size(1024, 768)
driver.get('https://google.com/')
time.sleep(5)


element = driver.find_element_by_id('lst-ib')
time.sleep(2)
driver.save_screenshot('testing.png')
element.send_keys('Python')
element.send_keys(Keys.ENTER)
time.sleep(2)
element = driver.find_element_by_link_text('Welcome to Python.org')
element.click()
time.sleep(2)
driver.save_screenshot('testing.png')
print(driver.current_url)
driver.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy _, 2016-04-21
@kani339

This is a phantom bug of the latest versions. On 1.9.8 - the script passes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question