Reputation: 503
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
Upvotes: 0
Views: 1194
Reputation: 101
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']"
Upvotes: 0
Reputation: 19
Use the operator +
if you're dealing with strings like
String myString1 = "Hey Dude..." + "How are you?";
Upvotes: 0
Reputation: 82755
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']")
Upvotes: 1