复制单元格值并粘贴到搜索框中

2024-04-26 04:34:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图设计一个自动化过程,读取excel文档中单元格的值,将值复制到变量中,然后将变量粘贴到站点上的搜索框中。当进程再次被调用时,它将转到下一行,获取新的单元格值,然后再次搜索。但是我一点也不明白!在

*编辑日期:2016年7月21日 我现在的问题是,在代码的每次迭代中,前一个单元格和新单元格的值都会被粘贴。例如,第一个单元格是42-7211,第二个单元格是45-7311,下次调用函数时,它粘贴的是“42-721145-7311”而不是“45-7311”

这是我的完整更新代码。一旦我进入正确的屏幕,我就使用这个函数 prod_选择要粘贴的搜索栏,然后调用stp转到下一个单元格。在

我的代码已更新

import unittest
import xlrd
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium import *
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException 
from datetime import date
from datetime import timedelta
import time
from time import sleep
def yesterday():
    # Get today.
    today = date.today()
    # Subtract timedelta of 1 day.
    yesterday = today - timedelta(days=1)
    return yesterday
import logging
#start logging module this will record all actions and record them for debugging purposes!
LOG_FILENAME = 'log_file_selenium.txt'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
logging.debug('This message should go to the log file')
#clear window
os.system('cls')
#open EXCEL 

#Start classes
#define variables to call for automation process
#this will help run faster so the code is shorter in the main sections
"""
Used to read cells from an excel file one at a time

Use:
    1) Call the class with new()
    2) Call new.var() to clear variables and start at top of document
    3) Call new.stp() to begin process
    4) Call new.nextp() until finished
"""
#########################################################################
#########################################################################


class calls_sms():

    def __init__(self): #sets variables up and inital value NULL
        self.w = None
        self.variable = None
        self.value = None
        self.default_int = 0          #
        self.default_string = None    #These are for empty feilds
        self.default = None           #

        #open EXCEL
        self.file_location = "C:\\Users\\doarni\\Desktop\\T0088Transfer.xls"
        self.workbook = xlrd.open_workbook(self.file_location)
        self.sheet = self.workbook.sheet_by_index(0)
        #Excel interaction

    def beginprod(self):
        self.w = 1
        calls_sms.stp(self)

    def stp(self):
        for row in range(self.sheet.nrows):
            row = self.sheet.row_values(self.w)
            self.variable = None
            self.variable = row[7]
            self.w += 1
            return row[7]


    #abbreviations for later use
    def var(self): #must be called var, driver is not defined till launch call
        self.xpath = self.driver.find_element_by_xpath
        self.classname = self.driver.find_element_by_class_name
        self.css = self.driver.find_element_by_css_selector
        self.actions = ActionChains(self.driver)


    #Open IE driver and new IE window
    def launch(self):
        self.driver = webdriver.Ie()
        self.driver.get("https://www.mywebsite.com/")
        logging.debug('Connected to SMS on '+str(date.today())+'')

    def login(self):
       self.username = self.classname("GJCH5BMD1C")
       self.password = self.xpath("//*[@type='password']")

       try:  
           self.username.send_keys("username")
           self.password.send_keys("password")
           self.log_in = self.xpath("//*[@class='GJCH5BMI-C']").click()
       except:
           os.system('python sms_generic_err.py')
           logging.debug('FAILED LOGIN'+str(date.today())+'')


    #Stock tab

    def stock_tab(self):
        #Only call when on stock tab
        self.stock = self.xpath("//div[@class='GJCH5BMHEF' and text()=' Stock ']").click()
        time.sleep(0.2)

    def stock_tab_prd_select(self):
        #opens prod chooser
        self.stockselect = self.xpath("//span[@class='GJCH5BMKU' and text()='Select']").click()

    def stock_searchbtn(self):
        self.stocksearch = self.xpath("//*[@class='GJCH5BMJV']")
        self.stocksearch.click()

    def stock_tab_prd_clear(self):
        #clears product
        self.stockclear = self.xpath("//span[@class='GJCH5BMKU' and text()='Clear']").click()

    def stock_resetbtn(self):
        #stock reset button
        self.stockreset = self.xpath("//span[@class='GJCH5BMKU' and text()='Reset']").click()        

    #Product Chooser

    def prod_choose_searchbar(self):            
        #finds the searchbox and clicks         
        self.search = self.css(".GJCH5BMASD")
        self.search.click()
        print('paste value: '+str(self.variable))
        #pastes in prod number from variable 
        self.clicksearch = self.actions.move_to_element(self.search).send_keys(calls_sms.stp(self)).perform() 
        print('paste value after: '+str(self.variable))


    def prod_choose_clicksearch(self):
        self.clicksearch = self.xpath("//*[@class='GJCH5BMPR']").click()

    def prod_choose_clickadd(self):
        self.clickadd = self.css(".GJCH5BMCHI").click()

    def prod_choose_clickfinish(self):
        self.clickfinish = self.xpath("//div[@class='GJCH5BMJYC GJCH5BMGYC' and text()='Finish']").click()



    #these must be called manually in the script not through the class
    def user_ask_invtab(): 
        os.system('python sms_err_select_inv.py')
    def user_ask_ciinsight():
        os.system('python sms_err_select_ciinsight.py')


if __name__ == "__main__":
    unittest.main()

#########################################################################
#########################################################################
#Check Territory 
        #NEEDS TO BE FIXED LATER!
        #this will most likely be built in another module
class territory_check():

    def check_exists_ci__by_xpath():
        try:
            webdriver.find_element_by_xpath("//*[@class='GJCH5BMOW']")
        except NoSuchElementException:
            return False
        os.system("python sms_connected_ci.py")
        return True

#########################################################################
#########################################################################


SMS = calls_sms()



#%#%#%#%#%#%#%#%#%#%#%#%
SMS.launch()
SMS.var()
SMS.login()
os.system('python sms_err_select_inv.py')
time.sleep(2)

SMS.stock_tab()
time.sleep(2)

SMS.beginprod()

SMS.stock_tab_prd_select()

time.sleep(1)

SMS.prod_choose_searchbar()

time.sleep(2)

SMS.prod_choose_clickfinish()

time.sleep(2)

SMS.stock_tab_prd_select()

time.sleep(1)

SMS.prod_choose_searchbar()

time.sleep(2)

SMS.prod_choose_clickfinish()

time.sleep(1)

SMS.stock_tab_prd_select()

time.sleep(1)

SMS.prod_choose_searchbar()

time.sleep(2)

SMS.prod_choose_clickfinish()

我想让它读取excel文件,从单元格中获取值,粘贴到send_键中。然后可以调用另一个函数移动到下面的下一个单元格,并且只在被调用时循环。在


Tags: andfromimportselftimedefseleniumstock

热门问题