首页 文章

Selenium不会在Python中打开浏览器

提问于
浏览
0

我是Python的新手,我正在尝试在Debian中使用Selenium,但它不起作用,更具体地说它似乎停留在循环中并且没有任何反应 . 下一个脚本是我用过的测试:

#!/usr/bin/env python 
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.python.org')

当我中断脚本时,会出现以下文本:

Traceback(最近一次调用最后一次):文件“prueba_parseo.py”,第7行,在browser = webdriver.Firefox()文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/ webdriver.py“,第154行,在init keep_alive = True中)文件”/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py“,第140行,在init self.start_session中(desired_capabilities,browser_profile)文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”,第229行,在start_session响应= self.execute(Command.NEW_SESSION,参数)文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”,第295行,执行响应= self.command_executor.execute(driver_command,params)文件“/ usr / local / lib / python2.7 / dist-packages / selenium / webdriver / remote / remote_connection.py“,第464行,执行返回self._request(command_info [0],url,body = data)文件”/ usr / local /lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py“,第488行,在_request resp = self._conn.getresponse()文件“/usr/lib/python2.7/httplib.py”,第1111行,在getresponse response.begin()文件“/usr/lib/python2.7/httplib .py“,第444行,在开始版本,状态,原因= self._read_status()文件”/usr/lib/python2.7/httplib.py“,第400行,在_read_status line = self.fp.readline(_MAXLINE 1)文件“/usr/lib/python2.7/socket.py”,第476行,在readline data = self._sock.recv(self._rbufsize)KeyboardInterrupt

我一直在寻找答案,但没有任何作用 . 我已经更改了软件包的版本,导出no_proxy =“localhost,127.0.0.1”

操作系统:Debian 5

Python:2.7

硒:3.5

Geckodriver:0.17.0

Firefox:52.0

我不知道还能做什么或改变什么 . 非常感谢你!

1 回答

  • 1

    我的猜测是它实际上一切顺利,浏览器在后台启动 . 它保持打开的原因可能是因为我可以在你的追溯中看到默认选项 keep_alive=True .

    完成测试后,尝试使用 browser.close()browser.quit() 关闭浏览器 .

    从文档:

    最后,浏览器窗口关闭 . 您也可以调用quit方法而不是close . 退出将退出整个浏览器,而close`将关闭一个选项卡,但如果只打开一个选项卡,默认情况下大多数浏览器将完全退出 .

    http://selenium-python.readthedocs.io/getting-started.html#simple-usage

相关问题