Python 3.x Termux中的Firefox Selenium

Python 3.x Termux中的Firefox Selenium,python-3.x,selenium,firefox,geckodriver,termux,Python 3.x,Selenium,Firefox,Geckodriver,Termux,我在termux中运行ubuntu,我安装pycharm来创建python代码。问题是我在操作webdriver时出错 我有最新的firefox(v59.0.2)、Selenium geckodriver v.0.24.0和python 3.6.5 这是代码的核心 from selenium import webdriver driver = webdriver.Firefox('/root/Downloads/geckodriver') 这里是错误 Traceback (most rece

我在termux中运行ubuntu,我安装pycharm来创建python代码。问题是我在操作webdriver时出错

我有最新的firefox(v59.0.2)、Selenium geckodriver v.0.24.0和python 3.6.5

这是代码的核心

from selenium import webdriver

driver = webdriver.Firefox('/root/Downloads/geckodriver')
这里是错误

Traceback (most recent call last):
   File "<input>", line 3, in <module>
   File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
firefox_profile = FirefoxProfile(firefox_profile)
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 80, in __init__
    ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock"))
  File "/usr/lib/python3.6/shutil.py", line 309, in copytree
names = os.listdir(src)
FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver'
    Traceback (most recent call last):
    File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver': '/root/Downloads/geckodriver'
During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "<input>", line 3, in <module>
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
这里是错误

Traceback (most recent call last):
   File "<input>", line 3, in <module>
   File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
firefox_profile = FirefoxProfile(firefox_profile)
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_profile.py", line 80, in __init__
    ignore=shutil.ignore_patterns("parent.lock", "lock", ".parentlock"))
  File "/usr/lib/python3.6/shutil.py", line 309, in copytree
names = os.listdir(src)
FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver'
    Traceback (most recent call last):
    File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver': '/root/Downloads/geckodriver'
During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "<input>", line 3, in <module>
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/root/PycharmProjects/untitled/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
回溯(最近一次呼叫最后一次):
文件“/root/PycharmProjects/untitled/venv/lib/python3.6/site packages/selenium/webdriver/common/service.py”,第76行,开头
标准偏差=管道)
文件“/usr/lib/python3.6/subprocess.py”,第709行,在__
恢复信号,启动新会话)
文件“/usr/lib/python3.6/subprocess.py”,第1344行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
FileNotFoundError:[Errno 2]没有这样的文件或目录:'/root/Downloads/geckodriver':'/root/Downloads/geckodriver'
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“”,第3行,在
文件“/root/PycharmProjects/untitled/venv/lib/python3.6/site packages/selenium/webdriver/firefox/webdriver.py”,第164行,在__
self.service.start()
文件“/root/PycharmProjects/untitled/venv/lib/python3.6/site packages/selenium/webdriver/common/service.py”,第83行,开头
os.path.basename(self.path)、self.start\u错误消息)
selenium.common.exceptions.WebDriverException:消息:“geckodriver”可执行文件需要位于路径中。

根据您的第一次代码试用,此错误消息

FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver'
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
…表示您的程序无法在上述目录中找到GeckoDriver

您可以在讨论中找到有关此错误的详细分析

根据您的第二次代码试用,此错误消息

FileNotFoundError: [Errno 2] No such file or directory: '/root/Downloads/geckodriver'
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
…表示您的程序无法在上述目录中找到GeckoDriver

您可以在讨论中找到有关此错误的详细分析

解决方案 理想情况下,您需要:

  • 确保GeckoDriver非root用户具有可执行权限
  • 以非root用户身份执行
    @Tests
  • 使用以下代码块:

    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path=r'/non_root_user/Downloads/geckodriver')
    driver.get("http://google.com/")
    driver.quit()
    

只需将geckodriver文件复制到您的路径中即可。 类型 echo$PATH
要找到你的路径

如果你尝试绝对路径会发生什么
executable\u path='C:/path\u to\u root/root/Downloads/geckodriver
我相信C驱动器只适用于windows,我在termux bud中使用ubuntu。驱动器是什么并不重要,它只是一个例子。好吧,鉴于我并不真正了解这些东西,如何做到这一点?我的意思是我不知道用
path\u to\u root
替换什么。我只知道我的文件系统以/root/开头,对于bullet 1和3,我有termux,它是一个
virtualenv
,所以它是唯一的
root
用户,除非我有办法检查非root用户,我检查了权限及其所有读写,我还尝试将其放入
/usr/local/bin
中,结果是
意外退出。状态代码是:-4
对于bullet 2,我不知道,我是个新手