selenium.webdriver - 名称错误:未定义名称"驱动程序"



我尝试搜索并尝试不同的方式,但没有成功。需要这里的专家成员的帮助。如何解决NameError:???

Traceback (most recent call last):
File "c:/Users/JM505/Desktop/Python/FSG Script/20200625/wei-master/wei.py", line 56, in <module>
date = driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div[1]/div[1]/div/div[1]/div[1]/div/div/button').text
NameError: name 'driver' is not defined

代码:

import datetime 
import random
import time
import os
import openpyxl
from selenium import webdriver


if(not os.path.exists('database.xlsx')):
time.sleep(5)
from openpyxl import Workbook
book = Workbook()
sheet = book.active
sheet['B27']='Day'
sheet['C27']='Drink'
sheet['D27']='Food'
sheet['E27']='Cake'
sheet['F27']='Date'
sheet['G27']='Total'
sheet['H27']='Item'
sheet['I27']='Debit'
sheet['J27']='Credit'
sheet['K27']='Balance'
book.save('database.xlsx')
else:
book = openpyxl.load_workbook('database.xlsx')
sheet = book.active
row=int(sheet.dimensions.split(':')[1][1:])+1
try:
f = open("username.txt", "r")
a=f.read()
f = open("password.txt", "r")
b=f.read()
f.close()
except:
print('please provide username and password')
try:
driver = webdriver.Chrome('chromedriver')
driver.get('https://squareup.com/login')    
username = driver.find_element_by_xpath('/html/body/div[1]/div/div/section/div[1]/div[3]/form/div[1]/input')
username.send_keys(a)
password = driver.find_element_by_xpath('/html/body/div[1]/div/div/section/div[1]/div[3]/form/div[3]/input')
password.send_keys(b)
sign_in_button = driver.find_element_by_xpath('/html/body/div[1]/div/div/section/div[1]/div[3]/form/div[6]/button')
sign_in_button.click()
time.sleep(random.randint(5,9))
driver.get('https://squareup.com/dashboard/sales/reports/category-sales')
time.sleep(random.randint(8,10))
#time.sleep(random.randint(8,10))
except:
print('unable to login trying again')
time.sleep(1)
date = driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div[1]/div[1]/div/div[1]/div[1]/div/div/button').text
print(date)
day_name= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
day = datetime.datetime.strptime(date, '%m/%d/%Y').weekday()
print(day_name[day])

正如JD2775所提到的,您应该将driver = webdriver.Chrome('chromedriver')移动到try之外,如果发生错误,第54行的except将向前移动并尝试获取driver对象,结果和错误,因此您需要在date = driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div[1]/div[1]/div/div[1]/div[1]/div/div/button').text上创建try except或像这个一样全局创建驱动程序

import datetime 
import random
import time
import os
import openpyxl
from selenium import webdriver

driver = webdriver.Chrome('chromedriver')
if(not os.path.exists('database.xlsx')):
time.sleep(5)
from openpyxl import Workbook
book = Workbook()
sheet = book.active
sheet['B27']='Day'
sheet['C27']='Drink'
sheet['D27']='Food'
sheet['E27']='Cake'
sheet['F27']='Date'
sheet['G27']='Total'
sheet['H27']='Item'
sheet['I27']='Debit'
sheet['J27']='Credit'
sheet['K27']='Balance'
book.save('database.xlsx')
else:
book = openpyxl.load_workbook('database.xlsx')
sheet = book.active
row=int(sheet.dimensions.split(':')[1][1:])+1
try:
f = open("username.txt", "r")
a=f.read()
f = open("password.txt", "r")
b=f.read()
f.close()
except:
print('please provide username and password')
try:
driver.get('https://squareup.com/login')    
username = driver.find_element_by_xpath('/html/body/div[1]/div/div/section/div[1]/div[3]/form/div[1]/input')
username.send_keys(a)
password = driver.find_element_by_xpath('/html/body/div[1]/div/div/section/div[1]/div[3]/form/div[3]/input')
password.send_keys(b)
sign_in_button = driver.find_element_by_xpath('/html/body/div[1]/div/div/section/div[1]/div[3]/form/div[6]/button')
sign_in_button.click()
time.sleep(random.randint(5,9))
driver.get('https://squareup.com/dashboard/sales/reports/category-sales')
time.sleep(random.randint(8,10))
#time.sleep(random.randint(8,10))
except:
print('unable to login trying again')
time.sleep(1)
date = driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div[1]/div[1]/div/div[1]/div[1]/div/div/button').text
print(date)
day_name= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
day = datetime.datetime.strptime(date, '%m/%d/%Y').weekday()
print(day_name[day])

相关内容

最新更新