b0122 python 爬虫 (二)-selenium-自动化测试

说明

使用chrome浏览器。

环境

windows

参考 selenium+python爬虫全流程教程  前面 的安装过程

思路

查看浏览器chrome 版本,这里是 98.0.4758.102, 去官网下载 驱动程序

安装python 依赖包 selenium,这里装完后是 4.3.0

 

linux

centos7.9 , hc2108

Python3 selenium在CentOS服务端运行

linux系统中安装python的selenium包 2022-04-19

思路

1、 安装linux下浏览器

2、安装 selenium 驱动

注意点:

  • 由于之前安装 python3, 执行yum 安装包过程有问题, 需要把相关 第一行 /usr/bin/python 改成 /usr/bin/python2.7
  • 赋权,这样hadoop用户就能使用   -rwxrwxrwx. 1 hadoop hadoop  12502264 7月  19 03:36 chromedriver

同样 的代码,在windows 改一下 chromedriver 路径, 也可以执行

 

使用

工程路径 D:\1_common\python\code\projects\Bigdata2022\Crawer

# -*- coding: utf-8 -*-
from selenium import webdriver
import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(r"E:\Programs\chromedriver_win32\chromedriver.exe")

driver.get("http://www.baidu.com")


driver.implicitly_wait(10) #

search_text = driver.find_element(By.ID, 'kw')

# 输入框输入内容
search_text.send_keys("seleniumm")

# 删除多输入的一个 m
search_text.send_keys(Keys.BACK_SPACE)


search_text.submit()

time.sleep(7)

driver.quit()
View Code

 

# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome(r"E:\Programs\chromedriver_win32\chromedriver.exe")

driver.get("http://www.baidu.com")

# 定位到要悬停的元素, 这里根据超链接的文本内容定位元素
above = driver.find_element(By.LINK_TEXT, '地图')

# move_to_element 这里有很多其他 动作, 鼠标、键盘都有
# 对定位到的元素执行鼠标悬停操作
ActionChains(driver).move_to_element(above).perform()



time.sleep(3)


above = driver.find_element(By.LINK_TEXT, '视频')

ActionChains(driver).move_to_element(above).perform()

time.sleep(10)

driver.quit()
View Code

 

# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

# driver = webdriver.Chrome(r"E:\Programs\chromedriver_win32\chromedriver.exe")

s = Service(r"E:\Programs\chromedriver_win32\chromedriver.exe")
driver = webdriver.Chrome(service=s)

driver.get("http://www.baidu.com")

# 每隔开0.5秒检查 指定元素是否出现,最多等5秒
element = WebDriverWait(driver, 5, 0.5).until(
                      EC.presence_of_element_located((By.ID, "kw"))
                      )
element.send_keys('selenium')
element.submit()

time.sleep(7)

driver.quit()
View Code

 

linux  下

# -*- coding: utf-8 -*-
from selenium import webdriver

from selenium.webdriver.chrome.options import Options

chrome_options = Options()

chrome_options.add_argument('--no-sandbox') #让Chrome在root权限运行

chrome_options.add_argument('--disable-dev-shm-usage') #不打开图形界面

chrome_options.add_argument('--headless') #浏览器不提供可视化页面

chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度

chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug

driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/opt/google/chrome/chromedriver') #Chrome驱动的位置,此学习记录中安装到了Chrome程序根目录,该路径为绝对路径

driver.get('https://www.baidu.com')

content = driver.page_source.encode('utf-8')

print(content)

driver.quit()
View Code

 

资料

Selenium Python 教程, 九四干, 2022-05-05

selenium的八种定位方式之:id、name、tag_name、class_name、link_text、partial_link_text、xpath,进击的小陈,2021-10-06

 

 
posted @ 2022-07-23 00:28  sunzebo  阅读(49)  评论(0编辑  收藏  举报