Reputation: 503
I have following code and not getting clue how to extract the days from the xpath and store into some lists with some indexes
Basically the xpath identifies only date but on my web page it contain both Date and Days and Days are not part of xpath.
dar = time.localtime(time.time())
das = time.localtime(time.time()- 86400)
dat = time.localtime(time.time() - (86400 * 2))
dau = time.localtime(time.time() - (86400 * 3))
dav = time.localtime(time.time() - (86400 * 4))
date = {"Date0": time.strftime("%Y-%m-%d",dar), "Date1": time.strftime("%Y-%m-%d",das), "Date2": time.strftime("%Y-%m-%d",dat), "Date3": time.strftime("%Y-%m-%d",dau),"Date4": time.strftime("%Y-%m-%d",dav)}
for i in range(4):
dayss = driver.find_element_by_xpath("//*[@id='"+date["Date"+str(i)]+"']").text
lists = dayss.split()
print(lists[0])
print("######")
print(lists[1])
I'm getting below output to make you understand whats on lists[0] and lists[1]
06-Sep
######
Fri
05-Sep
######
Thu
04-Sep
######
Wed
03-Sep
######
Tue
['Fri','Thu','Wed','Tue']
So that based on the above output I should able to access each index such as lists[0]
must return 'Fri'
and lists[1]
must return 'Thu'
and so on. But currently with lists[1]
it's returning all the days and not able to pass the iterator lists[i]
always getting index out of range
Following is the below html:
<td class="selected_date" id="2019-09-02" width="68" align="center">02-Sep<br>Mon</td>
Upvotes: 0
Views: 551
Reputation: 33384
Split the text with newline and append it to empty list.
dar = time.localtime(time.time())
das = time.localtime(time.time()- 86400)
dat = time.localtime(time.time() - (86400 * 2))
dau = time.localtime(time.time() - (86400 * 3))
dav = time.localtime(time.time() - (86400 * 4))
date = {"Date0": time.strftime("%Y-%m-%d",dar), "Date1": time.strftime("%Y-%m-%d",das), "Date2": time.strftime("%Y-%m-%d",dat), "Date3": time.strftime("%Y-%m-%d",dau),"Date4": time.strftime("%Y-%m-%d",dav)}
daylist=[]
for i in range(4):
dayss = driver.find_element_by_xpath("//*[@id='"+date["Date"+str(i)]+"']").text
lists = dayss.split("\n")
daylist.append(lists[1])
print(daylist)
Output:
['Fri', 'Thu', 'Wed', 'Tue']
Upvotes: 1
Reputation: 17593
Your xpath seems fine as your html tag have both value and so your are getting both values.
You need to declare one more list let say list2 and add only list[1] in each iteration for list2. Thats how at the end your list2 will contain only data index which you need
OR
You can use index in split function itself
In java it is like
tim.split(":")[0];
In python I guess it is like
tim.split(':', 1)
Upvotes: 0