python遇到使用selenium的问题,selenium是个


最近在学着使用blackwidow这个工具,在ubuntu20.02系统的安装过程当中遇到了selenium使用的一些问题。

selenium是个什么工具? 

Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera,Edge等。这个工具的主要功能包括:测试与浏览器的兼容性——测试应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成.Net、Java、Perl等不同语言的测试脚本

这一个工具同时也集成到了python的库当中,通过引用模块就可以使用。

不过如果遇到了以下这种错误:

/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
crawl.py:29: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome(chrome_options = chrome_options)
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 71, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "crawl.py", line 29, in <module>
    driver = webdriver.Chrome(chrome_options = chrome_options)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 89, in __init__
    self.service.start()
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

Selenium3.x调用浏览器必须有一个webdriver驱动文件,错误是no such file or directory: "chromedriver"的意思就是电脑没有安装chrome,也没有找到软件路径。需要先去官网上下载一下这个压缩包

parallels@parallels-Parallels-Virtual-Platform:~/BlackWidow$ sudo wget https://chromedriver.storage.googleapis.com/107.0.5304.18/chromedriver_linux64.zip
--2022-10-13 18:57:21--  https://chromedriver.storage.googleapis.com/107.0.5304.18/chromedriver_linux64.zip
Resolving chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)... 142.250.204.48
Connecting to chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)|142.250.204.48|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6876869 (6.6M) [application/zip]
Saving to: ‘chromedriver_linux64.zip’

chromedriver_linux64.zip                        100%[====================================================================================================>]   6.56M  5.34MB/s    in 1.2s    
2022-10-13 18:57:22 (5.34 MB/s) - ‘chromedriver_linux64.zip’ saved [6876869/6876869]

安装之后进行解压,然后就直接得到一个可执行文件,我们再继续运行原来的命令,又会遇到新的以下的问题

/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
crawl.py:29: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome(chrome_options = chrome_options)
Traceback (most recent call last):
  File "crawl.py", line 29, in <module>
    driver = webdriver.Chrome(chrome_options = chrome_options)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
    super().__init__(
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 272, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 364, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

找不到chrome binary,这种情况一般是浏览器对应的驱动没有再默认路径下找到浏览器的二进制文件。

首先我们检查到主机上就没有安装谷歌浏览器

parallels@parallels-Parallels-Virtual-Platform:~/BlackWidow$ chromium-browser -version
Command 'chromium-browser' not found, but can be installed with:
sudo apt install chromium-browser
1. parallels@parallels-Parallels-Virtual-Platform:~/BlackWidow$ sudo apt install chromium-browser
2. 将之前下载的文件移动到该路径/usr/bin,命令: sudo mv chromedriver /usr/bin
3. 切换到/usr/bin目录,命令: cd /usr/bin
4. 使得其变成可执行文件,命令 :sudo chmod a+x chromedriver

最后再执行工具命令就可以了

本文来自博客园,作者:ivanlee717,转载请注明原文链接:https://www.cnblogs.com/ivanlee717/p/16789389.html

相关内容