Selenium脚本配置(hinas_魔百盒m201-2)

起初想通过直接安装在系统里面,但是多番尝试失败了,后来想直接用docker一键部署算球。

hinas_魔百盒m201-2启用docker一件脚本:

install-docker.sh

更换docker源一键脚本

bash <(curl -sSL https://gitee.com/xjxjin/scripts/raw/main/check_docker_registry.sh)

拉取镜像

docker pull selenium/standalone-firefox

启动镜像容器(容器界面https://hub.docker.com/r/selenium/standalone-firefox

docker run -d -p 4444:4444 -p 7900:7900 –shm-size=”2g” selenium/standalone-firefox:latest

测试代码

from selenium import webdriver
from selenium.webdriver import DesiredCapabilities

if __name__ == '__main__':
    driver = webdriver.Remote(
        # 指定远程浏览器的地址
        command_executor='http://localhost:4444/wd/hub',
        # 指定期望的浏览器类型,这里安装的是 firefox 所以指定firefox
        desired_capabilities=DesiredCapabilities.FIREFOX,
    )
    # 发送请求
    driver.get("https://www.baidu.com")
    # 获取网页源码
    html = driver.page_source
    print(html)
    # 获取网页title
    title = driver.title
    print(title)  # 百度一下,你就知道
    # 获取当前网页的 url
    current_url = driver.current_url
    print(current_url)  # https://www.baidu.com/
    # 退出驱动,关闭所有关联的窗口
    driver.quit()

测试代码2

from selenium import webdriver

driver = webdriver.Firefox(executable_path=r'C:\Users\<YourUsername>\Desktop\geckodriver.exe') # 替换为您实际的路径
driver.get("http://www.example.com")

print(driver.title)

Comments

No comments yet. Why don’t you start the discussion?

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注