I am making a reddit bot that will look for certain attributes in comments, use selenium to visit the information website, and use driver.find_elements_by...
to get the value inside those tags.
Now, driver.find_elements_by...
is not iterable, and there are multiple <span class="name">Lorem Ipsum</span>
tags with text inside them that I want obtained. I am storing this as a variable and replying to the comment via PRAW.
Suppose that the HTML is this:
<span class="name">Lorem</span>
<span class="name">Ipsum</span>
<span class="name">Dolor</span>
<span class="name">Sit</span>
<span class="name">Amet</span>
So, how could I obtain the text from all of the <span class="name">
tags, and when I store it as a variable and reply, will it just put all the text together without spaces or will it format it with a space between each text, supposing that I write:
tags = driver.find_element_by...
comment.reply("Tags: {}".format(tags))
And if it just puts all the text together, how can I format it so that there are spaces?
To extract the texts e.g. Lorem, Ipsum, Dolor, Sit, Amet, etc from all of the
<span>
using Selenium and python you have to induce WebDriverWait forvisibility_of_all_elements_located()
and you can use either of the following Locator Strategies:Using
CSS_SELECTOR
andget_attribute("innerHTML")
:Using
XPATH
and text attribute:Console Output:
Note : You have to add the following imports :
Outro
Link to useful documentation:
get_attribute()
methodGets the given attribute or property of the element.
text
attribute returnsThe text of the element.