1. 定位一组元素
格式:find_elements_by_xxxx()
- find_elements_by_id()
- find_elements_by_name()
- find_elements_by_class_name()
- find_elements_by_tag_name()
- find_elements_by_link_text()
- find_elements_by_partial_link_text()
- find_elements_by_xpath()
- find_elements_by_css_selector()
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/1/17 3. # @Author : 大海 4. 5. from selenium import webdriver 6. from time import sleep 7. 8. driver = webdriver.Chrome() 9. driver.get("https://www.baidu.com") 10. 11. driver.find_element_by_id("kw").send_keys("selenium") 12. driver.find_element_by_id("su").click() 13. sleep(1) 14. 15. # 定位一组元素 16. texts = driver.find_elements_by_xpath('//div/h3/a') 17. 18. # 循环遍历出每一条搜索结果的标题 19. for t in texts: 20. print(t.text) 21. 22. # 定位一组元素根据索引取想要的值,索引从0开始,这取第二个 23. text1 = driver.find_elements_by_xpath('//div/h3/a')[1].text 24. print(text1) 25. 26. driver.quit()