安装相关插件(1,2步骤只演示Chrome。Firefox类似)
1,使用rz命令导入下载好的google-chrome-stable_current_x86_64.rpm,
并安装 yum -y install google-chrome-stable_current_x86_64.rpm
2,安装chromedriver
(1)通过 yum list | grep chrom 来查看你当前谷歌浏览器属于哪一个版本
(2)去 http://chromedriver.storage.googleapis.com/index.html 中下载一个对应版本的zip文件下来
(3)同样通过rz 将文件导入进来
(4)并用unzip进行解压(举例:unzip chromedriver_linux64.zip),解压之后你会看到一个 chromedriver 的文件夹
(5)授权文件夹权限为最大 chmod 777 chromedriver
(6)移动文件夹到你的环境目录下
cp chromedriver /usr/bin/chromedriver
3,python环境安装 selenium包
(1)进入python环境。(我这里是去家目录下,然后执行 source venv/bin/activate 。我是去的我虚拟版本中,你们根据自己环境来就好)
(2)安装selenium包
pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple
python 代码书写
# 导入 seleniun 相关类
from selenium import webdriver
from selenium.webdriver.common.by import By
#导入属性设置类!!!!!
from selenium.webdriver.chrome.options import Options
# 在linux系统中 需要设置一些参数来规避一些信息
chrome_options = Options()
# 设置浏览器的无头浏览模式 无头无界面 那在linux下就可以运行了
chrome_options.add_argument('--headless')
# 避免 DevToolsActivePort 不存在报错的形式
chrome_options.add_argument('--no-sandbox')
# 官方关闭的一些关键选项,规避一些BUG
chrome_options.add_argument('--disable-gpu')
# 创建一个linux的谷歌浏览器,并将设置的属性传入(windows中直接不用设置这个属性)
test_webdriver = webdriver.Chrome(options=chrome_options)
# 模拟百度打开百度 并进行过搜索
test_webdriver.get('https://www.baidu.com/')
# 模拟输入框输入文字
test_webdriver.find_element_by_xpath('//input[@id="kw"]').send_keys('python')
# 模拟按钮查询
test_webdriver.find_element_by_xpath('//input[@id="su"]').click()
# 打印标题
print(test_webdriver.title)
# 关闭谷歌浏览器
test_webdriver.quit()
3万+

被折叠的 条评论
为什么被折叠?



