Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x I';m运行基于Selenium的代码时出现名称错误_Python 3.x_Selenium_Selenium Webdriver_Nameerror - Fatal编程技术网

Python 3.x I';m运行基于Selenium的代码时出现名称错误

Python 3.x I';m运行基于Selenium的代码时出现名称错误,python-3.x,selenium,selenium-webdriver,nameerror,Python 3.x,Selenium,Selenium Webdriver,Nameerror,我正在使用Selenium web驱动程序进行一个项目。预期的结果是它应该打开google.com。 这是我得到的错误 C:\Users\Admin\PycharmProjects\Rohan\venv\Scripts\python.exe "C:/Users/Admin/Rohan_Python/Selenium Web Driver.py" Traceback (most recent call last): File "C:\Users\Admin\Roha

我正在使用Selenium web驱动程序进行一个项目。预期的结果是它应该打开google.com。 这是我得到的错误

C:\Users\Admin\PycharmProjects\Rohan\venv\Scripts\python.exe 
"C:/Users/Admin/Rohan_Python/Selenium Web Driver.py"
Traceback (most recent call last):
File "C:\Users\Admin\Rohan_Python\Selenium Web Driver.py", line 8, in <module>
self.driver.get(url="https://www.google.com/")
NameError: name 'self' is not defined

请帮帮我,给我解决办法。谢谢

1检查缩进和空行。按照惯例,缩进应使用4个空格和2个空行:

from selenium import webdriver


class Info:
    def __init__(self):
        self.driver = webdriver.Chrome(executable_path='your path')
查看此处了解有关init的更多信息

有关缩进检查的更多详细信息,请参见:

下一步,还要修改代码。别忘了缩进

def get_info(self, query):
    self.query = query
    self.driver.get(url="https://www.google.com/")
  • 类名应以大写字母开头
  • 从最容易复制的代码开始,然后一步一步地使其更复杂。这是初学者的好例子

  • 看起来您在缩进方面有问题
    def get_info(self, query):
        self.query = query
        self.driver.get(url="https://www.google.com/")