0

How can I concatenate a variable with another string variable in Selenium? For example, Date0,Date1,Date2..etc are variables and have the actual time and date.
Here is the code:

dar = time.localtime(time.time())
das = time.localtime(time.time() + 86400)
Date0 = time.strftime("%Y-%m-%d",dar)
Date1 = time.strftime("%Y-%m-%d",das)

The problem in Selenium I'm facing while executing the Python script

for i in range(5):
        for j in range(4,7):
            if (str(i) == '0' or str(i) == '1' or str(i) == '2' or str(i) == '3' or str(i) == '4')and(str(j) == '4' or str(j) == '5' or str(j) == '6'):
                hrs_fill = driver.find_element_by_xpath("//*[@id='"+ Date+str(i) +"_50"+ str(j) +"_166003-5_hrs']")
                hrs_fill.send_keys('3')
                break

Getting the error:

NameError: name 'Date' is not defined

It's considering Date as separate variable and str(i) as separate variable not as combined

1
  • try str(Date+str(i)) Commented Aug 28, 2019 at 12:21

3 Answers 3

1

Use a dict

Ex:

import time
dar = time.localtime(time.time())
das = time.localtime(time.time() + 86400)
date = {"Date0": time.strftime("%Y-%m-%d",dar), "Date1": time.strftime("%Y-%m-%d",das)}
print(date["Date"+str(0)])

date = {"Date0": time.strftime("%Y-%m-%d",dar), "Date1": time.strftime("%Y-%m-%d",das)}
hrs_fill = driver.find_element_by_xpath("//*[@id='"+ date["Date"+str(i)] +"_50"+ str(j) +"_166003-5_hrs']")
Sign up to request clarification or add additional context in comments.

2 Comments

Problem is in xpath for identifying both the variable as separate variables rather than combined variable
Thanks , it worked. I have a small query,with that I am able to iterate 1st column and corresponding 3 rows are working but 2nd columns onwards columns are getting iterated but rows are not getting iterated
0

Use the operator + if you're dealing with strings like

String myString1 = "Hey Dude..." + "How are you?";

Comments

0

You create variables called Date0 and Date1, but you called only 'Date' on the code. Try:

"//*[@id='"+ Date0+str(i) +"_50"+ str(j) +"_166003-5_hrs']"

or

"//*[@id='"+ Date1+str(i) +"_50"+ str(j) +"_166003-5_hrs']"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.