Home > Software design >  How can I .send_keys(v.S1) a variable?
How can I .send_keys(v.S1) a variable?

Time:02-06

I have only been programing or coding or whatever it is that I am doing for a few days and could use a hand figuring out what I need to research to figure out what I am trying to do. I am working on a project for charity so I dont really want to learn all kinds of things I will probably never use again so I was hoping someone could tell me how to do this or point me in the direction of what I need to learn to make this happen.

So I have created a crawler that goes and types text into a search bar, Eggs for example and then takes me to the eggs results and captures the data, brand, price, count ect.

searchBox.send_keys(v.S1)

My problem is I can not figure out how to change v.S1 into V.S2 so I can automate going thru many searches without having to copy an paste the code over and over again.

I am working with a main.py to call the functions, a functions.py to store the functions and a variables.py to store the list of variables as S1-S2-S3 ect.

I have been able to to get searchBox.send_keys(v.S1) to work as searchBox.send_keys(X) with a variable X = v.S1 but for the life of me I can not figure out how to add 1 to make X = v.S2 after the function completes the first search.

So far all the information I have needed has been under the same By.CLASS_NAME but I have set those to a variable as well since I may need to change some of those on a per case basis as I go as well.

Well any help or someone pointing me in the right direction would be appreciated. Thanks.

CodePudding user response:

To pass a character sequence e.g. v.S1, v.S2, v.S3, etc you can use the range() function and use argument-unpacking operator i.e. * and finally append it to the constant string v.S

elements = [*range(1,6,1)]
elements = ['v.S{0}'.format(element) for element in elements]
print(elements)

# prints -> ['v.S1', 'v.S2', 'v.S3', 'v.S4', 'v.S5']

CodePudding user response:

I suggest you to add all the S1,S2.... variables in a list or tuple and then iterate using a simple for loop like,

# varlist is a list containing all variables
for i in varlist:
    searchBox.send_keys(i)
  • Related