Exception while executing PhantomJS + Selenium Webdriver + Python program

With all the prerequisites for installing PhantomJS and Selenium on my Ubuntu machine, I run below code snippet:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get("https://duckduckgo.com/")
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
print driver.current_url
driver.quit()

      

On execution I get below error:

$ python duck.py 
Traceback (most recent call last):
  File "duck.py", line 5, in <module>
    driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 208, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 664, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 175, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Error Message => 'Unable to find element with id 'search_form_input_homepage''
 caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"107","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:50789","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"sessionId\": \"26560250-fec9-11e4-b2ee-2dada5838664\", \"value\": \"search_form_input_homepage\"}","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/26560250-fec9-11e4-b2ee-2dada5838664/element"}
Screenshot: available via screen

      

+3


source to share


2 answers


Setting up --ssl-protocol=any

the service argument
and using Explicit Waits did it for me:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.PhantomJS(service_args=['--ssl-protocol=any'])

driver.maximize_window()
driver.get("https://duckduckgo.com/")

wait = WebDriverWait(driver, 10)
search = wait.until(EC.presence_of_element_located((By.ID, "search_form_input_homepage")))
search.send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()

print driver.current_url
driver.quit()

      



Printing https://duckduckgo.com/?q=realpython

.

Note that without --ssl-protocol=any

PhantomJS did not even load the page, but the current url remained about:blank

.

+2


source


I think what is happening to you is that you are trying to find an element that has not yet loaded on the page. So I can recommend inserting a WaitForElementDisplayed(by.ID("search_form_input_homepage"));

right before you try to type in the search field. This way you make sure the element exists before trying to interact with it.



I cannot give you an example code because I am not very familiar with Python bindings.

0


source







All Articles