Selenium: Iterating through groups of elements

前端 未结 1 1363
眼角桃花
眼角桃花 2020-12-23 19:16

I\'ve done this with BeautifulSoup but it\'s a bit cumbersome, and I\'m trying to figure out if I can do it directly with Selenium.

Let\'s say I have the following H

相关标签:
1条回答
  • 2020-12-23 19:37

    Use find_elements_by_class_name() to get all blocks and find_element_by_xpath() to get title and company for each person:

    persons = []
    for person in driver.find_elements_by_class_name('person'):
        title = person.find_element_by_xpath('.//div[@class="title"]/a').text
        company = person.find_element_by_xpath('.//div[@class="company"]/a').text
    
        persons.append({'title': title, 'company': company})
    
    0 讨论(0)
提交回复
热议问题