Selenium与Python脚本

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

Selenium with python script

问题

我是新手使用selenium,一直遇到相同的错误。我正在使用Windows操作系统,selenium版本是v4.6.0。

from selenium import webdriver

driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://www.selenium.dev/')
print(driver.title)
driver.quit()

但我一直遇到这个错误,驱动程序的路径是正确的。

错误信息

Traceback (most recent call last):
  File "c:\Users\chris\development\automation\selenium\index.py", line 5, in <module>
    driver = webdriver.Chrome('chromedriver.exe')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\chris\development\automation\selenium\.venv\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 49, in __init__
    super().__init__(
  File "C:\Users\chris\development\automation\selenium\.venv\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 60, in __init__
    ignore_proxy=self.options._ignore_local_proxy,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_ignore_local_proxy'
英文:

I'm new to selenium and I keep running into the same error. I'm working on a windows os and have selenium version is v4.6.0.

from selenium import webdriver

driver = webdriver.Chrome(&#39;chromedriver.exe&#39;)
driver.get(&#39;https://www.selenium.dev/&#39;)
print(driver.title)
driver.quit()

But I keep running into this error and the path for the driver is correct.

Error

Traceback (most recent call last):
  File &quot;c:\Users\chris\development\automation\selenium\index.py&quot;, line 5, in &lt;module&gt;
    driver = webdriver.Chrome(&#39;chromedriver.exe&#39;)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;C:\Users\chris\development\automation\selenium\.venv\Lib\site-packages\selenium\webdriver\chrome\webdriver.py&quot;, line 49, in __init__
    super().__init__(
  File &quot;C:\Users\chris\development\automation\selenium\.venv\Lib\site-packages\selenium\webdriver\chromium\webdriver.py&quot;, line 60, in __init__
    ignore_proxy=self.options._ignore_local_proxy,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: &#39;str&#39; object has no attribute &#39;_ignore_local_proxy&#39;

答案1

得分: 1

你需要一个绝对路径来使用Selenium的Chrome驱动器。
你的问题已经在这里得到了回答。

英文:

You need an absolute path to chrome driver for selenium to work.
Your question is answered here.

答案2

得分: 0

问题出在下面这行代码:

driver = webdriver.Chrome('chromedriver.exe')

如果你的selenium版本是v4.6.0或更高版本,那么你不需要设置chromedriver.exe的路径,所以你的代码可以简化为以下形式:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.selenium.dev/')
print(driver.title)
driver.quit()

参考链接:

英文:

The issue is in the below line:

driver = webdriver.Chrome(&#39;chromedriver.exe&#39;)

If your selenium version is v4.6.0 or above, then you don't need to set the path for chromedriver.exe, so your code can be simplified as below:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get(&#39;https://www.selenium.dev/&#39;)
print(driver.title)
driver.quit()

References:

答案3

得分: 0

从参数中删除 executable_url,因为如果您安装了Selenium的最新版本,且Selenium版本高于4.6.0,则无需添加 executable_url。在最新版本的Selenium中,您无需下载webdriver

只需复制下面的代码并运行您的Python文件:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://www.facebook.com/")
英文:

Note: Remove executable_url from the argument, because you have installed the latest version of Selenium if you have selenium above the 4.6.0 you don't need to add executable_url and in the latest version of Selenium you don't need to download webdriver.

just copy the below code and run the your python file simple

from selenium import webdriver

driver=webdriver.Chrome()

driver.get(&quot;https://www.facebook.com/&quot;) 

huangapple
  • 本文由 发表于 2023年6月29日 16:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579252.html
匿名

发表评论

匿名网友

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

确定