简体   繁体   中英

selenium.webdriver - NameError: name 'driver' is not defined

I try search and try with different way but not working. would need some help from expert member here.how to resolve 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

Code:

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])

As JD2775 mentioned you should move driver = webdriver.Chrome('chromedriver') outside of try , and if error occured except on line 54 will just move forward and try to get driver object, resulting and error, so you need to try except on 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 or create driver globally like this

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM