请求未按预期工作,Selenium 未正确安装。

huangapple go评论191阅读模式
英文:

Requests not working as intended and Selenium not installed properly

问题

I am trying to import the html code for a certain website (https://www.trendyol.com/de/s/alle-marken). Since requests didn't work as intended (not finding all the code), I was advised to try to download Selenium to try a different approach. My first question is, why did requests not find all the code? I was told it has something to do with JavaScript, but I am not familiar with the language and didn't understand the explanation.
我试图导入某个网站的HTML代码(https://www.trendyol.com/de/s/alle-marken)。由于请求未按预期工作(未找到所有代码),我被建议尝试下载Selenium来尝试不同的方法。我的第一个问题是,为什么请求未找到所有代码?我被告知这与JavaScript有关,但我对这种语言不熟悉,不理解解释。

My second problem is that I am having problems installing selenium and the chrome web driver. I used
我的第二个问题是,我在安装Selenium和Chrome Web驱动程序时遇到问题。我使用了以下命令来安装每个包:

python3.11 -m pip install selenium

and

python3.11 -m pip install webdriver-manager

to install each package, and downloaded matching chrome and chromedriver versions (116.0.5829.0 (r1156474)) from this website:
来安装每个包,并从此网站下载了匹配的Chrome和Chromedriver版本(116.0.5829.0(r1156474)):

https://googlechromelabs.github.io/chrome-for-testing/

I also put all the files from the new chrome installation as well as the webdriver folder in this path:
我还将来自新的Chrome安装以及webdriver文件夹的所有文件放在以下路径下:

C:\Program Files\Google\Chrome\Application

However, when I try to run the code given in the website:
然而,当我尝试运行网站上提供的代码时:

import time
from selenium import webdriver

driver = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chromedriver-win64\chromedriver.exe')
driver.get('http://www.google.com/')

time.sleep(5)  # Let the user actually see something!

search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()

time.sleep(5)  # Let the user actually see something!

driver.quit()

I get the following error message:
我收到以下错误消息:

Traceback (most recent call last):
  File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\common\driver_finder.py", line 42, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\common\selenium_manager.py", line 74, in driver_location
    browser = options.capabilities["browserName"]
              ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Guilherme Candeias\Downloads\scraper.py", line 4, in <module>
    driver = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chromedriver-win64\chromedriver.exe')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\chrome\webdriver.py", line 47, in __init__
    self.service.path = DriverFinder.get_path(self.service, self.options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\common\driver_finder.py", line 44, in get_path
    raise NoSuchDriverException(f"Unable to obtain {service.path} using Selenium Manager; {err}")
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain chromedriver using Selenium Manager; 'str' object has no attribute 'capabilities'; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

I tried installing other versions of chrome, but could not find chromedriver versions that support my system (win64). I also tried using:
我尝试安装其他版本的Chrome,但找不到支持我的系统(win64)的Chromedriver版本。我还尝试使用以下代码:

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

But I only got a different error message:
但我只收到不同的错误消息:

Traceback (most recent call last):
  File "c:\Users\Guilherme Candeias\Downloads\trendyol scraper.py", line 5, in <module>
    driver = webdriver.Chrome(ChromeDriverManager().install())
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\chrome.py", line 39, in install
    driver_path = self._get_driver_path(self.driver)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\core\manager.py", line 30, in _get_driver_path
    file = self._download_manager.download_file(driver.get_driver_download_url())
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz

<details>
<summary>英文:</summary>

I am trying to import the html code for a certain website (https://www.trendyol.com/de/s/alle-marken). Since requests didn&#39;t work as intended (not finding all the code), I was advised to try to download Selenium to try a different approach. My first question is, why did requests not find all the code? I was told it has something to do with JavaScript, but I am not familiar with the language and didn&#39;t understand the explanation.
My second problem is that I am having problems installing selenium and the chrome web driver. I used 
`python3.11 -m pip install selenium`
and
`python3.11 -m pip install webdriver-manager`
to install each package, and downloaded matching chrome and chromedriver versions (116.0.5829.0 (r1156474)) from this website:
https://googlechromelabs.github.io/chrome-for-testing/

I also put all the files from the new chrome installation as well as the webdriver folder in this path:
C:\Program Files\Google\Chrome\Application

However, when I try to run the code given in the website:

import time
from selenium import webdriver

driver = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chromedriver-win64\chromedriver.exe')
driver.get('http://www.google.com/')

time.sleep(5) # Let the user actually see something!

search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()

time.sleep(5) # Let the user actually see something!

driver.quit()


I get the following error message:


Traceback (most recent call last):
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\common\driver_finder.py", line 42, in get_path
path = SeleniumManager().driver_location(options) if path is None else path
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\common\selenium_manager.py", line 74, in driver_location
browser = options.capabilities["browserName"]
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\Users\Guilherme Candeias\Downloads\scraper.py", line 4, in <module>
driver = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chromedriver-win64\chromedriver.exe')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\chrome\webdriver.py", line 47, in init
self.service.path = DriverFinder.get_path(self.service, self.options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\common\driver_finder.py", line 44, in get_path
raise NoSuchDriverException(f"Unable to obtain {service.path} using Selenium Manager; {err}")
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain chromedriver using Selenium Manager; 'str' object has no attribute 'capabilities'; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location


I tried installing other versions of chrome, but could not find chromedriver versions that support my system (win64). I also tried using:

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

But I only got a different error message:

Traceback (most recent call last):
File "c:\Users\Guilherme Candeias\Downloads\trendyol scraper.py", line 5, in <module>
driver = webdriver.Chrome(ChromeDriverManager().install())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\chrome.py", line 39, in install
driver_path = self._get_driver_path(self.driver)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\core\manager.py", line 30, in _get_driver_path
file = self._download_manager.download_file(driver.get_driver_download_url())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\drivers\chrome.py", line 40, in get_driver_download_url
driver_version_to_download = self.get_driver_version_to_download()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\core\driver.py", line 51, in get_driver_version_to_download
self._driver_to_download_version = self._version if self._version not in (None, "latest") else self.get_latest_release_version()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\drivers\chrome.py", line 62, in get_latest_release_version
resp = self._http_client.get(url=latest_release_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\core\http.py", line 37, in get
self.validate_response(resp)
File "C:\Users\Guilherme Candeias\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\webdriver_manager\core\http.py", line 16, in validate_response
raise ValueError(f"There is no such driver by url {resp.url}")
ValueError: There is no such driver by url https://chromedriver.storage.googleapis.com/LATEST_RELEASE_116.0.5829



</details>


# 答案1
**得分**: 0

如果您的Selenium版本是`v4.6.0`或更高,则不需要再设置`chromedriver.exe`的路径。

因此,您的代码可以简化如下:

driver = webdriver.Chrome()
driver.get('http://www.google.com/')


参考此链接 - https://stackoverflow.com/a/76463081/7598774

<details>
<summary>英文:</summary>

If you selenium version is `v4.6.0` or above, you don&#39;t really need to set the path of `chromedriver.exe` anymore.

So your code can be reduced to below:

driver = webdriver.Chrome()
driver.get('http://www.google.com/')


Refer this - https://stackoverflow.com/a/76463081/7598774

</details>



huangapple
  • 本文由 发表于 2023年6月19日 08:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定