Selinium安装麻烦

时间:2016-11-19 15:01:29

标签: python selenium

我正在努力正确安装selenium,我试图运行Selenium文档提供的测试脚本。我安装了python 3和python 2,是否有可能强制pip安装到python 2.7而不是3?也许那是我的问题?任何帮助或指导将不胜感激。

测试代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

1:首先我用pip安装了硒

sudo pip install selenium

2:我试图运行测试脚本

python TestSelenium.py 

追踪错误:

 Traceback (most recent call last):
  File "TestSelenium.py", line 1, in <module>
    from selenium import webdriver
ImportError: No module named selenium

3:现在,如果我尝试使用python3,它会发现它安装在/usr/lib/python3.5/site-packages/selenium

python3 TestSelenium.py 

TraceBack错误:

    Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/usr/lib64/python3.5/subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.5/subprocess.py", line 1544, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "TestSelenium.py", line 4, in <module>
    driver = webdriver.Firefox()
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
    self.service.start()
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x7f9f9f184c18>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 163, in __del__
    self.stop()
  File "/usr/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'

1 个答案:

答案 0 :(得分:0)

要使用selenium Firefox webdriver,您必须下载geckodriver。您可以从&#34; https://github.com/mozilla/geckodriver/releases&#34;下载它。

然后指定geckodriver路径,如下所示:

browser = webdriver.Firefox(executable_path= your_geckogriver_path)

我认为这会奏效。请试一试。