记录一次centos 使用selenium运行环境

这里写自定义目录标题

宝塔面板 安装 selenium

安装google-chrome

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

查看chrome版本 google-chrome --version

下载对应chrome版本的chromedriver
wget http://npm.taobao.org/mirrors/chromedriver/88.0.4324.27/chromedriver_linux64.zip
https://googlechromelabs.github.io/chrome-for-testing/#stable
在这里插入图片描述

将下载的文件解压,放在如下位置
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/
给予执行权限
chmod +x /usr/bin/chromedriver

运行代码,验证是否成功,linux必须是这样的(新建一个.py文件,拷进去)
#!/user/bin/python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
ch_options = Options()
ch_options.add_argument(“–headless”) # => 为Chrome配置无头模式
ch_options.add_argument(‘–no-sandbox’)
ch_options.add_argument(‘–disable-gpu’)
ch_options.add_argument(‘–disable-dev-shm-usage’)
browser = webdriver.Chrome(options=ch_options)
browser.get(“http://www.baidu.com”)
print(browser.title)

from selenium import webdriver
import time
import json
import re

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



options = webdriver.ChromeOptions()  # 创建浏览器对象之前,创建options功能对象
options.add_argument("--headless")  # 添加无界面功能参数
driver = webdriver.Chrome(options=options)  # 构造浏览器对象,打开浏览器



# 创建webdriver 对象,指明使用chrome 浏览器驱动
# driver = webdriver.Chrome()


# 调用webdriver 对象的get方法,可以让浏览器打开指定网址
driver.get('https://URL')
# 首先清除由于浏览器打开已有的cookies
# wd.delete_all_cookies()
# time.sleep(10)
# 打开cookie文本,使用已保存的cookie登录
with open('cookies.txt', 'r') as f:
    # 使用json读取cookies 注意读取的是文件 所以用load而不是loads
    cookies_list = json.load(f)
    for cookie in cookies_list:
        driver.add_cookie(cookie)



# time.sleep(3)
driver.implicitly_wait(2)
# 等待按钮出现并点击
try:

    # 等待快速验证按钮出现,这里使用显式等待
    verify_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CLASS_NAME, 'verifyBtn'))
    )
    # 模拟点击快速验证按钮
    verify_btn.click()



    print("快速验证按钮已点击")
except Exception as e:
    print("点击快速验证按钮时出错:", e)

time.sleep(1)

# driver.refresh()  # 刷新页面

# 打印网页渲染后的源代码
# print(driver.page_source)















相关推荐

  1. )window使用VMware运行Centos7

    2024-02-07 06:52:02       45 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-07 06:52:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-07 06:52:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-07 06:52:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-07 06:52:02       20 阅读

热门阅读

  1. 大语言模型训练数据集(1)

    2024-02-07 06:52:02       36 阅读
  2. 【STAT4052】Introduction to Statistical Learning

    2024-02-07 06:52:02       24 阅读
  3. swift结算体系

    2024-02-07 06:52:02       35 阅读
  4. 14.Swift函数

    2024-02-07 06:52:02       28 阅读
  5. ChatGPT学习大纲

    2024-02-07 06:52:02       34 阅读
  6. 数据结构刷题 -- 客房预约

    2024-02-07 06:52:02       29 阅读
  7. 动态数据源

    2024-02-07 06:52:02       27 阅读
  8. CGAL::2D Arrangements-2

    2024-02-07 06:52:02       28 阅读
  9. vscode代码快捷键

    2024-02-07 06:52:02       27 阅读
  10. 【Kotlin】自定义Json反序列化

    2024-02-07 06:52:02       32 阅读