[Python2.7]用phantomjs和selenium遇到错误(TypeError: getaddrinfo() argument 1 must be string or None)

  • 错误:
  File "youku_ckey.py", line 45, in youku_ckey
    driver = webdriver.PhantomJS(executable_path=bce_exec_path,desired_capabilities=desired_capabilities,service_args=service_args)
  File "/home/cyl/lib/python2.7/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 62, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/cyl/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 148, in __init__
    self.command_executor = RemoteConnection(command_executor, keep_alive=keep_alive)
  File "/home/cyl/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 120, in __init__
    port=port)
  File "/home/cyl/lib/python2.7/site-packages/selenium/webdriver/common/utils.py", line 72, in find_connectable_ip
    connectable = is_connectable(port, sockaddr[0])
  File "/home/cyl/lib/python2.7/site-packages/selenium/webdriver/common/utils.py", line 106, in is_connectable
    socket_ = socket.create_connection((host, port), 1)
  File "/home/cyl/lib/python2.7/socket.py", line 554, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
TypeError: getaddrinfo() argument 1 must be string or None
  • 解决:
    (1). 打开 /home/cyl/lib/python2.7/socket.py
    (2). 如错误,定位554行发现
    getaddrinfo的方法,
    打印出来这个位置的数据:

《[Python2.7]用phantomjs和selenium遇到错误(TypeError: getaddrinfo() argument 1 must be string or None)》 1

发现:
host有一些非string的值,所以需要修改源代码:

《[Python2.7]用phantomjs和selenium遇到错误(TypeError: getaddrinfo() argument 1 must be string or None)》 2

再次测试,发现成功了~

by 炎隆

    原文作者:_小老虎_
    原文地址: https://www.jianshu.com/p/4a8d12f208af
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞