使用 Python 在线加入 Class 的机器人给出无效的 NameError
An Bot To Join Online Class With Python Is Giving Invalid NameError
这是我的代码:
#importing necessary modules for our program.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
#Fill In Your email and password in the CREDS dictionary.
CREDS = {"email" : "9714@sindhimodel.in", "password" : "DhruvSev2006"}
URL = "https://teams.microsoft.com"
def start_browser():
#This Function Will Start Your Browser.
PATH = "C:\Program Files (x86)\chromedriver.exe"
chrome = webdriver.Chrome(PATH)
chrome.get(URL)
print("Browser Opened. \n")
login()
def login():
#This Function Will Login To Your Account, Using The CREDS dictionary.
time.sleep(2)
print("Logging In To Your Account. \n")
EmailField = chrome.find_element_by_id("i0116")
EmailField.send_keys(CRED['email'])
next = chrome.find_element_by_id("idSIButton9")
next.click()
time.sleep(2)
pswd = chrome.find_element_by_id("i0118")
pswd.send_keys(CRED['password'])
sign_in = chrome.find_element_by_id("idSIButton9")
sign_in.click()
time.sleep(2)
web_app = chrome.find_element_by_class_name("use-app-lnk")
web_app.click()
time.sleep(5)
print("Logged In To Your Account. \n")
def change_class():
#This Function Is Related To My School And My Classes. You Can Delete It. :)
print("Changing Format Of Classes. \n")
clas = input("which Class Do You Want To Join: ")
classes = [["maths", "english", "p/c", "biology", "csc", "social"], ["hindi"]]
if clas in classes[0]:
clas = "Class:9:All Subjects Except Hindi"
elif clas in classes[1]:
clas = "Class:9:Hindi"
else:
clas = ""
raise KeyError("Class Not Found :(")
return clas
def know_class():
#This Will Ask The User For The Class, Which The User Wants To Join.
clas = change_class()
if clas == "Class:9:All Subjects Except Hindi":
team = "9 General"
elif clas == "Class:9:Hindi":
team = "9 HINDI General"
else:
team = ""
def join_other_class():
general_9 = chrome.find_element_by_title_name("ts-ongoing-call-header is-call-message")
general_9.click()
start_browser()
know_class()
join_other_class()
在 login() 函数(第 20 行)中,在 start_browser() 函数(第 79 行)中调用时给出错误。
chrome = webdriver.Chrome(PATH)
给出的错误(当运行命令提示符(CMD)中的代码在Windows中)是:
Traceback (most recent call last):
File "C:\Users\DHRUV\Desktop\New folder\o.py", line 79, in <module>
start_browser()
File "C:\Users\DHRUV\Desktop\New folder\o.py", line 20, in start_browser
login()
File "C:\Users\DHRUV\Desktop\New folder\o.py", line 27, in login
EmailField = chrome.find_element_by_id("i0116")
NameError: name 'chrome' is not defined
请帮帮我。我已经定义了 chrome
但它仍然报错。
代码仍未完成,但仍然想检查代码是否能正常工作。
也请给出改进代码的方法。这是匆忙写的。而且我也是初学者
提前致谢!
您需要做的是将 chrome
指定为全局变量,您可以通过两种不同的方式来做到这一点,首先是将这段代码放在您的函数之外,让它成为一个可用的变量像这样的所有功能
PATH = "C:\Program Files (x86)\chromedriver.exe"
chrome = webdriver.Chrome(PATH)
def start_browser():
...
或者这样做
def start_browser():
#This Function Will Start Your Browser.
PATH = "C:\Program Files (x86)\chromedriver.exe"
global chrome
chrome = webdriver.Chrome(PATH)
chrome.get(URL)
print("Browser Opened. \n")
login()
Here 你可以找到更多关于全局变量的信息
chrome
是start_browser()
中的局部变量。
如果您想通过其他方法访问它 login()
,您有多种选择:
- 将其作为参数传递
- 使用全局变量(如果是第一次赋值,可能需要在函数中使用
global
关键字)
- 写一个class并使
chrome
成为一个实例属性,而不是单独的函数,这样你就可以在所有其他实例方法中使用self.chrome
访问它
我个人的偏好是选项 3,但您可以选择任何您想要的选项,甚至可以选择与我的建议完全不同的选项。
这是我的代码:
#importing necessary modules for our program.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
#Fill In Your email and password in the CREDS dictionary.
CREDS = {"email" : "9714@sindhimodel.in", "password" : "DhruvSev2006"}
URL = "https://teams.microsoft.com"
def start_browser():
#This Function Will Start Your Browser.
PATH = "C:\Program Files (x86)\chromedriver.exe"
chrome = webdriver.Chrome(PATH)
chrome.get(URL)
print("Browser Opened. \n")
login()
def login():
#This Function Will Login To Your Account, Using The CREDS dictionary.
time.sleep(2)
print("Logging In To Your Account. \n")
EmailField = chrome.find_element_by_id("i0116")
EmailField.send_keys(CRED['email'])
next = chrome.find_element_by_id("idSIButton9")
next.click()
time.sleep(2)
pswd = chrome.find_element_by_id("i0118")
pswd.send_keys(CRED['password'])
sign_in = chrome.find_element_by_id("idSIButton9")
sign_in.click()
time.sleep(2)
web_app = chrome.find_element_by_class_name("use-app-lnk")
web_app.click()
time.sleep(5)
print("Logged In To Your Account. \n")
def change_class():
#This Function Is Related To My School And My Classes. You Can Delete It. :)
print("Changing Format Of Classes. \n")
clas = input("which Class Do You Want To Join: ")
classes = [["maths", "english", "p/c", "biology", "csc", "social"], ["hindi"]]
if clas in classes[0]:
clas = "Class:9:All Subjects Except Hindi"
elif clas in classes[1]:
clas = "Class:9:Hindi"
else:
clas = ""
raise KeyError("Class Not Found :(")
return clas
def know_class():
#This Will Ask The User For The Class, Which The User Wants To Join.
clas = change_class()
if clas == "Class:9:All Subjects Except Hindi":
team = "9 General"
elif clas == "Class:9:Hindi":
team = "9 HINDI General"
else:
team = ""
def join_other_class():
general_9 = chrome.find_element_by_title_name("ts-ongoing-call-header is-call-message")
general_9.click()
start_browser()
know_class()
join_other_class()
在 login() 函数(第 20 行)中,在 start_browser() 函数(第 79 行)中调用时给出错误。
chrome = webdriver.Chrome(PATH)
给出的错误(当运行命令提示符(CMD)中的代码在Windows中)是:
Traceback (most recent call last):
File "C:\Users\DHRUV\Desktop\New folder\o.py", line 79, in <module>
start_browser()
File "C:\Users\DHRUV\Desktop\New folder\o.py", line 20, in start_browser
login()
File "C:\Users\DHRUV\Desktop\New folder\o.py", line 27, in login
EmailField = chrome.find_element_by_id("i0116")
NameError: name 'chrome' is not defined
请帮帮我。我已经定义了 chrome
但它仍然报错。
代码仍未完成,但仍然想检查代码是否能正常工作。
也请给出改进代码的方法。这是匆忙写的。而且我也是初学者
提前致谢!
您需要做的是将 chrome
指定为全局变量,您可以通过两种不同的方式来做到这一点,首先是将这段代码放在您的函数之外,让它成为一个可用的变量像这样的所有功能
PATH = "C:\Program Files (x86)\chromedriver.exe"
chrome = webdriver.Chrome(PATH)
def start_browser():
...
或者这样做
def start_browser():
#This Function Will Start Your Browser.
PATH = "C:\Program Files (x86)\chromedriver.exe"
global chrome
chrome = webdriver.Chrome(PATH)
chrome.get(URL)
print("Browser Opened. \n")
login()
Here 你可以找到更多关于全局变量的信息
chrome
是start_browser()
中的局部变量。
如果您想通过其他方法访问它 login()
,您有多种选择:
- 将其作为参数传递
- 使用全局变量(如果是第一次赋值,可能需要在函数中使用
global
关键字) - 写一个class并使
chrome
成为一个实例属性,而不是单独的函数,这样你就可以在所有其他实例方法中使用self.chrome
访问它
我个人的偏好是选项 3,但您可以选择任何您想要的选项,甚至可以选择与我的建议完全不同的选项。