Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Python Selenium定位一组元素

WebDriver还提供了8种用于定位一组元素的方法。

  • 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()

定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词element后面多了一个s表示复数。

接下来通过例子演示定位一组元素的使用:

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")

driver.find_element_by_id("kw").send_keys("AppBlog.CN")
driver.find_element_by_id("su").click()
sleep(1)

# 定位一组元素
texts = driver.find_elements_by_xpath('//div/h3/a')

# 循环遍历出每一条搜索结果的标题
for t in texts:
    print(t.text)

driver.quit()

程序运行结果:

APP开发技术博客
应用中心-Z-BlogPHP & Z-Blog 2后台资源大本营,提供Z-BlogPHP & Z...
Sina App Engine Blog | 无处不在 触手可及
博客园 - 开发者的网上家园
新浪博客首页_新浪网
BLOG APP 推广页
Xamarin Android 打造属于自己的博客园APP(4) - IT胡小帅 - 博客园
whatsapp blog - WhatsApp 博客
app端维持用户登录状态 - CSDN博客
新浪博客首页_新浪网
app 【猪八戒网APP开发】专业平台 满意付款
上一篇 Python Selenium设置元素等待
下一篇 Python Selenium多表单切换