[英]Why do I get this error :AttributeError: 'Fetch_Info' object has no attribute 'driver' when I run my selenium_web.py script
I want to launch the web browser from my script file but when I run my code it gives me an error as 'AttributeError: 'Infow' object has no attribute 'driver'.我想从我的脚本文件中启动 web 浏览器,但是当我运行我的代码时,它给了我一个错误,因为“AttributeError:‘Infow’object 没有属性‘驱动程序’。
I tried finding a solution to this by uninstalling the current version of selenium 4.4.0 and reinstalling a previous version by running the command pip install selenium==4.0.0.b4
but this did not solve my problem.我尝试通过卸载当前版本的 selenium 4.4.0 并通过运行命令
pip install selenium==4.0.0.b4
重新安装以前的版本来找到解决方案,但这并没有解决我的问题。 My code is supposed to open the web browser when I run it but it is not.我的代码应该在我运行它时打开 web 浏览器,但事实并非如此。 How do I fix this?
我该如何解决?
My code:我的代码:
import time
from selenium import webdriver
from selenium.webdriver import chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
class Fetch_Info:
def __int__(self):
self.driver = webdriver.Chrome(
executable_path="C:\\Users\Whizzy.Ellah\\PycharmProjects\\Voice_Recognition_Virtual_Assistan\\chromedriver.exe")
def get_info(self, query):
self.query = query
self.driver.get(url='https://www.wikipedia.org')
search = self.driver.find_element_by_xpath('//*[@id="searchInput"]')
search.click()
search.send_keys(query)
enter = self.driver.find_element_by_xpath('//*[@id="search-form"]/fieldset/button')
enter.click()
assist = Fetch_Info()
assist.get_info("hello")
The error:错误:
File "C:\Users\Whizzy.Ellah\PycharmProjects\Voice_Recognition_Virtual_Assistant\selenium_web.py", line 19, in <module>
assist.get_info("hello")
"C:\Users\Whizzy.Ellah\PycharmProjects\Voice_Recognition_Virtual_Assistant\selenium_web.py",
line 15, in get_info
self.driver.get(url='https://www.wikipedia.org')
AttributeError: 'Fetch_Info' object has no attribute 'driver'
@Rukamakama below is the error I'm having after making the correction下面的@Rukamakama 是我在更正后遇到的错误
C:\Users\Whizzy.Ellah\PycharmProjects\Voice_Recognition_Virtual_Assist
ant\selenium_web.py:10: DeprecationWarning: executable_path has been
deprecated, please pass in a Service object
self.driver = webdriver.Chrome(
Traceback (most recent call last):
File
"C:\Users\Whizzy.Ellah\Desktop\virtual_assistant_env\lib\site-
packages\selenium\webdriver\common\service.py", line 74, in
start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program
Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file
specified
During handling of the above exception, another exception
occurred:
Traceback (most recent call last):
File `"C:\Users\Whizzy.Ellah\PycharmProjects\Voice_Recognition_Virtual_Assistant\selenium_web.py", line 23, in <module>
assist = Fetch_Info()`
File"C:\Users\Whizzy.Ellah\PycharmProjects\Voice_Recognition_Virtual_Assistant\selenium_web.py", line 10, in __init__
self.driver = webdriver.Chrome(
File "C:\Users\Whizzy.Ellah\Desktop\virtual_assistant_env\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\Whizzy.Ellah\Desktop\virtual_assistant_env\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 90, in init self.service.start()文件“C:\Users\Whizzy.Ellah\Desktop\virtual_assistant_env\lib\site-packages\selenium\webdriver\chromium\webdriver.py”,第 90 行,在init self.service.start()
File "C:\Users\Whizzy.Ellah\Desktop\virtual_assistant_env\lib\site-packages\selenium\webdriver\common\service.py", line 84, in start raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home文件“C:\Users\Whizzy.Ellah\Desktop\virtual_assistant_env\lib\site-packages\selenium\webdriver\common\service.py”,第 84 行,开始引发 WebDriverException(selenium.common.exceptions.WebDriverException:消息: 'chromedriver.exe' 可执行文件需要在 PATH 中。请参阅https://chromedriver.chromium.org/home
The __init__()
function is missing a i
. __init__()
function 缺少i
。 Please update your code from:请从以下位置更新您的代码:
def __int__(self)
to:至:
def __init__(self)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.