from selenium import webdriver import time import csv from selenium.webdriver.common.action_chains import ActionChains data = csv.reader(open(".\\PRODUCT.csv", 'r')) driver = webdriver.Chrome() # 登录后台 driver.get('http://172.16.2.80:8222/shop-web/index.html#/login') driver.maximize_window() # 登錄 driver.find_elements_by_xpath('//input[@placeholder="账号"]')[0].send_keys('admin') driver.find_elements_by_xpath('//input[@placeholder="密码"]')[0].send_keys('admin123') # 點擊登錄按鈕登錄 b = driver.find_element_by_xpath('//*[@id="app"]/div/form[1]/div[4]/div/button') b.click() time.sleep(3) # 點擊商品菜單 driver.find_element_by_xpath('//div/*[text()="商品"]').click() time.sleep(3) # 點擊添加商品菜單 driver.find_element_by_xpath('//span[text()="商品列表"]').click() time.sleep(3) for text in data: try: driver.find_elements_by_xpath('//input[@placeholder="商品名称"]')[0].clear() driver.find_elements_by_xpath('//input[@placeholder="商品名称"]')[0].send_keys(text[0]) driver.find_elements_by_xpath('//input[@placeholder="商品货号"]')[0].clear() driver.find_elements_by_xpath('//input[@placeholder="商品货号"]')[0].send_keys(text[1]) driver.find_element_by_xpath('//button//span[text()=" 查询结果 "]').click() time.sleep(3) product_name = driver.find_element_by_xpath('//p[@style="white-space: pre-wrap;"]') product_name = product_name.text print('查看商品成功:'+ product_name) filename = 'product_name' + '.csv' with open(filename,'a')as file_object: file_object.write(product_name + '\n') except: print('查看商品不成功:'+ text[0] + text[1] ) continue