
1. 前言

2. 爬取标题的脚本
py
脚本,它的功能是:
-
打开chrome浏览器 -
打开百度首页 -
点击“百度热搜” -
获取热搜标题并print出来
# -*- encoding=utf8 -*-
__author__ = "AirtestProject"
from airtest.core.api import *
auto_setup(__file__)
# 初始化并打开chrome浏览器
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome
driver = WebChrome()
driver.implicitly_wait(20)
# 打开百度首页
driver.get("https://www.baidu.com/")
# 点击百度热搜并切换到新标签页
driver.find_element_by_xpath("//*[@id=\"s-hotsearch-wrapper\"]/div/a/div/i").click()
driver.switch_to_new_tab()
# 打印百度热搜榜的标题
for hot in driver.find_elements_by_class_name("c-single-text-ellipsis"):
print(hot.text)

3. 命令行运行Web自动化脚本
1)python环境准备
2)chrome与chromedriver版本对应
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 98
Current browser version is 108.0.5359.73 with binary path C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
3)命令行运行

4. 小结



本文分享自微信公众号 - AirtestProject(AirtestProject)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。