Selenium获取具有相同类名的列表元素文本

时间:2017-02-02 19:46:17

标签: selenium selenium-webdriver

Html代码是:

<div class="table_body">
  <div>
    <div class="cell" title="Name">Name</div>
    <div class="cell" title="Email" >Email</div>
    <div class="cell" title="Action">Action</div>
  </div>
  <div class="sep_row"></div>
  <div>
    <div class="cell" title="abc">abc</div>
    <div class="cell" title="abc@br.com">abc@br.com</div>
    <div class="fa fa-trash-o"></div>
  </div>
  <div>
     <div class="cell" title="cuser3">cuser3</div>
     <div class="cell" title="auser2@bkr.com">auser2@bkr.com</div>
     <div class="fa fa-trash-o></div>
  </div>
  <div>
     <div class="cell" title="cuser" >cuser</div>
     <div class="cell" title="user@test.com" >user@test.com</div>
     <div class="fa fa-trash-o></div>
  </div>
</div>

如何获取电子邮件列表。 class =“table_body”的每个子元素都有不同的文本。我是在python中编写selenium测试的新手。

1 个答案:

答案 0 :(得分:3)

要获取电子邮件列表,请尝试:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("URL")
list_of_elements = driver.find_elements_by_xpath('//div[@class="cell"][contains(@title, "@")]')
emails = [email.text for email in list_of_elements]