【问题标题】:Exception on execution PhantomJS + Selenium Webdriver + Python program执行 PhantomJS + Selenium Webdriver + Python 程序时出现异常
【发布时间】:2015-05-20 08:27:21
【问题描述】:

在我的 Ubuntu 机器上安装了 PhantomJS 和 Selenium 的所有先决条件后,我在代码 sn-p 下运行:

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

执行时出现以下错误:

$ 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

【问题讨论】:

    标签: python selenium selenium-webdriver phantomjs headless-browser


    【解决方案1】:

    设置--ssl-protocol=any service argument 并使用Explicit Waits 使它对我有用:

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

    打印https://duckduckgo.com/?q=realpython

    请注意,如果没有--ssl-protocol=any,PhantomJS 甚至还没有加载页面,并且当前 url 保持为about:blank

    【讨论】:

      【解决方案2】:

      我认为发生在你身上的事情是你试图在页面上找到一个尚未加载的元素。所以我可以推荐的是在您尝试输入搜索字段之前插入WaitForElementDisplayed(by.ID("search_form_input_homepage"));。因此,在尝试与之交互之前,您将确定该元素存在。

      我不能真正给你一个代码示例,因为我并不真正熟悉 Python 绑定。

      【讨论】:

      • 好吧,异常说要么元素不存在,要么我们使用错误的标识符来查找它。现在,假设我们的标识符是正确的,剩下的就是元素不存在的另一个选项。只是为了尝试,您可以在页面加载时和尝试键入之前插入 Thread.Sleep(5000) 。这将在下一次交互之前等待 5 秒。
      • 不,问题是页面甚至没有在 PhantomJS 中加载。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2016-11-15
      • 2019-03-02
      • 2016-09-16
      • 2017-05-14
      • 1970-01-01
      相关资源
      最近更新 更多