简体   繁体   中英

Copy cell value and paste into search box

I'm trying to design an automation process that reads the value of a cell in an excel document, copies the value into a variable, and that variable is is pasted into a search box on a site. When the process is called again it goes to the next line, gets the new cell value, and searches again. However I can't seem to get it right in the slightest!

*Edit 7/21/2016 My current problem now is that on every iteration of the code the previous cell and new cell values are pasted. For example the first cell is 42-7211 and the second cell is 45-7311 and on the next time the function is called it pastes " 42-721145-7311 " not "45-7311"

Here is my full updated code. Once i get to the proper screen I use the function prod_choose_searchbar to paste and then call stp to go to the next cell.

My Code UPDATED

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

I want it to read the excel file, grab the value from the cell, paste it into send_keys. and then be able to call another function to move to the next cell below and loop only when called.

I figured it out finally. I have to update the actions variable or else the strings are stored in it each time. will post solution soon once its written

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