I'm new to Python, so I was hoping somebody could help me out with the following issue.
project_tags = browser.find_elements_by_xpath('//a[contains(@class, "campaignTags-tag")]')
separator = ', '
project_tags = separator.join(project_tags)
I know that if project_tags
were an array, the join
method would work. Hence I thought there may've been a chance the code above would've worked since project_tags
is an iterable, but of course it didn't since it's an iterable of FirefoxWebElements
.
I believe all I need to do in order to get join
to work in this context is to point to the text of each FirefoxWebElement
. Even though I can accomplish the aim in a couple other ways, I was just curious if this is possible?
One of those ways, which seems kind of verbose, is:
project_tags = browser.find_elements_by_xpath('//a[contains(@class, "campaignTags-tag")]')
for idx, tag in enumerate(project_tags):
if idx is 0:
tags_string = tag.text
else:
tags_string = tags_string + ', ' + tag.text if idx < len(project_tags) else tags_string + tag.text
Are there any methods I might not know about that would allow me to refactor and minimize the working example above?