Say that I have a piece of HTML code that looks like this:
<html>
<body>
<thspan class="sentence">He</thspan>
<thspan class="sentence">llo</thspan>
</body>
</html>
And I wanted to get the content of both and connect them into a string in Python Selenium.
My current code looks like this:
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
thspans = browser.find_elements(By.CLASS_NAME, "sentence")
context = ""
for thspan in thspans:
context.join(thspan.text)
The code can run without any problem, but the context variable doesn’t contain anything. How can I get the content of both and connect them into a string in Python Selenium?
2
Answers
context += thspan.text
instead of usingcontext.join(thspan.text)
just like @Rajagopalan saidYou were not redirecting the browser to the page you actually want to scrape the data from. And you were misusing the
.join
method. Here is a code that will work for you: