Home>
# get element with selenium
driver.find_elements_by_class_name ('wrap_box') [2] .find_elements_by_tag_name ('article')
# I want to combine the 10 elements that I got into text
(<selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "981d9fa1-7aa7-4f03-9191-9d8424ea1f15")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "d8ab2b29-ad93-4a1e-a62c-5678e3bb3baa")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "e4d5558c-c5a6-49eb-aafa-7e1903e5061f")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "3d48e296-23b1-4ae6-9fa6-0d6718618e51")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "ca920fa7-4ee3-4044-910e-7621dbf4eb4a")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "a4d1fb42-9bec-4492-9870-50a942478784")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "b086f1eb-ffac-424e-be45-83c2a13afcba")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "5a9bd3bd-13b2-4728-aeb3-23d327902793")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "4f1abc9d-03cf-461e-84db-24bf89cfe7cd")>,
 <selenium.webdriver.remote.webelement.WebElement (session = "c5478f098015ed3c442d6f9a2a200d5a", element = "98ae5249-1dd6-4c20-b136-2fa8f51201df")>]

# It is possible to loop by for statement and combine it by adding text and adding
text = ''
for i in driver.find_elements_by_class_name ('wrap_box') [2] .find_elements_by_tag_name ('article'):
 text + = i.text


We are considering a combination method other than the above, but we have not been able to find it.

  • join () can only handle strings
  • If i convert the acquired element to str type with map (), it will not be possible to convert it to text

I'm sure it's a nasty question, but I'd be happy if you could tell me.
Thanks for your cooperation.

  • Answer # 1

    You should combine comprehensions and joins.

    joined_text = 'Connect'.join ([article_tag.text for article_tag in driver.find_elements_by_class_name (' wrap_box ') [2] .find_elements_by_tag_name (' article ')])
    print (joined_text)

    If the text of each article element is a, b, c, ...
    The output result of print is a connection, connection b, connection c, y connection z.
    If you don't want to use letters to connect, change "Connect" to "".