【python】docker-selenium 分布式selenium模拟浏览器 |可视化 或 后台运行selenium 部署与使用

一、分布式selenium

1、部署 docker-selenium

Github官方地址如下:
https://github.com/SeleniumHQ/docker-selenium?tab=readme-ov-file

执行安装指令:

1、这里可以将dashboard映射接口改为 14444(记得开放安全组)

bash 复制代码
docker run --rm -it -p 14444:4444 -p 7900:7900 --shm-size 2g selenium/standalone-chrome

2、UI界面

访问网址:

1、这里映射的是14444端口

html 复制代码
http://127.0.0.1:14444/ui/

点击 《Session》tabs

点击摄像头按钮

默认密码:secret

可以看到模拟浏览器正在运行的效果

3、编程调用 docker-selenium

bash 复制代码
def login_get_cookie():
    # 配置浏览器选项
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')

    # 连接到远程Selenium Chrome节点
    browser = webdriver.Remote("http://127.0.0.1:14444", options=chrome_options)

    try:
        browser.get("https://xxbb.com/Account/Login")
        # 输出网页标题
        print(browser.title)
        account_name = '123'
        passward = '456'
        # 找到用户名和密码输入框,并模拟输入账号密码(请替换为实际的元素定位方式)
        # driver.find_element(By.ID,"validemail2").send_keys(account_name)
        print("Waiting for element to be clickable...")
        element = WebDriverWait(browser, 20).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, '[id="validemail2"]'))
        )
        element.send_keys(account_name)
        # print(browser.page_source)
        print("Element is clickable.")
        print("---------------获取到元素准备登录")
        browser.find_element(By.XPATH, '/html/body/div[1]/div/div/div/div[2]/div/form/div[2]/input').send_keys(passward)
        browser.find_element(By.ID, "button").click()

        # # 提交登录表单(如果需要点击登录按钮的话)
        # login_button = driver.find_element_by_css_selector('input[type="submit"]')  # 或其他定位方式
        # login_button.click()

        # 等待页面加载完成(如果需要的话,可以使用WebDriverWait配合expected_conditions进行等待)
        # ...
        # print("----------", browser.get_cookies())
        # 获取当前页面的所有Cookie
        cookies = browser.get_cookies()
        # print(cookies)
        # 初始化一个空字符串,用于存储拼接后的cookie字符串
        cookie_string = ""
        for cookie in cookies:
            print(f"Name: {cookie['name']}, Value: {cookie['value']}")
            # 将每个cookie的name和value拼接成形如"name=value"的格式,并添加到cookie_string中
            cookie_string += f"{cookie['name']}={cookie['value']};"
            # 去除最后一个分号
        cookie_string = cookie_string[:-1]
        return cookie_string
    finally:
        browser.quit()
相关推荐
金銀銅鐵7 小时前
[Python] 用 turtle 来绘制瑞典国旗
python
Albert Edison7 小时前
【GUI 自动化测试】Pywinauto 常见操作
自动化测试·python·pywinauto
海兰7 小时前
【高速缓存】RedisVL为文本生成嵌入向量实践指南
人工智能·python·机器学习
2601_955760077 小时前
如何用 Claude Opus 5 API 批量扩展长尾关键词和文章选题
前端·python·搜索引擎
艾斯特_8 小时前
从模型调用到可用聊天应用:会话状态与消息协议
python·ai·aigc
Marst Code8 小时前
Python 3.9 已停止维护!从 3.9 到 3.14 全版本深度对比,生产环境该选哪个?
开发语言·python
llwszx8 小时前
【Java/Go后端手撸原生Agent(第九篇):Plan-and-Execute规划模式——从“走一步看一步“到“先谋后动“】
java·python·golang·agent开发·plan模式·规划执行模式
lxw18449125148 小时前
Python uv 完整使用教程
python·conda·pip·uv
hangyuekejiGEO8 小时前
临沂GEO技术解析与行业应用方案
人工智能·python
取谖慕12.9 小时前
Docker容器从入门到精通
docker·容器·eureka