Reputation: 373
Individually the prints
in my for loops correctly print the items I want, but I'm having difficulties printing them together on the same line.
#Grabbing text from the first column in the table that contains "Elephant"
for cell in driver.find_elements_by_xpath("//*[contains(text(),'Elephant')]"):
ElepantText = cell.text
print(ElephantText)
#This prints:
#Elephant 1
#Elephant 2
#Elephant 3 etc...which is what I want
for element in driver.find_elements_by_xpath("//[contains(text(),'Elephant')]/following::td[1]/span/select[1]"):
selected = Select(element).first_selected_option
select_text = selected.text
print(select_text)
#This acquires the selected option in the dropdown menu following the cell that contains "Elephant" and prints the selected option which is what I want.
I tried:
print(ElephantText, select_text)
But this just returns the last value in ElephantText and none of the select_text Selected options.
I also tried to zip the two together using:
zipped = zip(ElephantText, select_text)
print(zipped)
But it returns this:
<zip object at 'random hexadecimal number'>
I tried turning these into lists again, but it just turned each letter in the result into an item within the list, so I'm kind of out of ideas at this point. Any direction would be appreciated.
EDIT
This is what I'd like my results to look like:
Elephant 1 - Selected
Elephant 2 - Selected
Elephant 3 - Selected
Upvotes: 0
Views: 47
Reputation: 2502
ElephantText and selected_text are strings. You cannot zip them. You need to store all the text values (if you're iterating over the collections one after the other) and then zip the list of text values:
ElephantTexts = []
for cell in driver.find_elements_by_xpath("//*[contains(text(),'Elephant')]"):
ElephantText = cell.text
print(ElephantText)
ElephantTexts.append(EelephantText)
Selected_texts = []
for element in driver.find_elements_by_xpath("//[contains(text(),'Elephant')]/following::td[1]/span/select[1]"):
selected = Select(element).first_selected_option
select_text = selected.text
print(select_text)
Selected_texts.append(selected_text)
merged = tuple(zip(ElephantTexts, Selected_texts)) # assuming they are the same size
for tup in merged:
print(tup)
I ran the following code with hardcoded lists:
ElephantTexts = ['Elephant1', 'Elephant2', 'Elephant3']
Selected_texts = ['Selected1', 'Selected2', 'Selected3']
merged = tuple(zip(ElephantTexts, Selected_texts)) # assuming they are the same size
for tup in merged:
print(tup)
and this is the output:
('Elephant1', 'Selected1')
('Elephant2', 'Selected2')
('Elephant3', 'Selected3')
Upvotes: 1