繁体   English   中英

selenium.webdriver - NameError:名称“驱动程序”未定义

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

我尝试搜索并尝试不同的方式但不起作用。 需要专家成员的帮助。如何解决 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之外,如果except第 54 行之外发生错误,将继续前进并尝试获取driver object,结果和错误,因此您需要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或像这样全局创建驱动程序

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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