selenium+PhantomJS+IP代理

简介: 首先安装selenium、PhantomJSselenium安装pip install seleniumPhantomJS安装这个需要手动的到官网下载开始使用导入需要的包import randomfrom selenium import webdriverfrom selenium.

首先安装selenium、PhantomJS

  • selenium安装

    pip install selenium
  • PhantomJS安装
    这个需要手动的到官网下载

开始使用

  • 导入需要的包

    import random
    from selenium import webdriver
    from selenium.webdriver.common.proxy import ProxyType
    # 调用键盘按键操作
    from selenium.webdriver.common.keys import Keys
    # 调用鼠标操作
    from selenium.webdriver import ActionChains
    # 设置请求头
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  • 操作PhantomJS

    desired_cap = DesiredCapabilities.PHANTOMJS.copy()
    # 设置请求头
    desired_cap['phantomjs.page.settings.User-Agent'] = 'user_agent...'
    # 配置代理IP
    proxy = [
        '--proxy=%s' % "139.199.38.177:8118",  # 设置的代理ip
        '--proxy-type=http',  # 代理类型
        '--ignore-ssl-errors=true',
        ]
    # 启动PhantomJS
    driver = webdriver.PhantomJS(executable_path="./phantomjs-2.1.1-linux-x86_64/bin/phantomjs",
                                     desired_capabilities=desired_cap,
                                     service_args=proxy)
    # 设置屏幕大小
    driver.set_window_size(800, 600)
    # 请求网页
    driver.get("http://www.baidu.com/")
    # 根据html元素的id找到搜索框,并且输入要搜索的内容
    driver.find_element_by_id('kw').send_keys("时光不写博客")
    # 查到搜索按钮,并且点击搜索
    random.choice([
                # 使用鼠标点击操作
                driver.find_element_by_id('su').click(),
                # 使用键盘回车操作
                driver.find_element_by_id('su').send_keys(Keys.RETURN)
                ])
    # 截屏
    driver.save_screenshot('baidu.png')
    # 使用xpath查到标签,并且点击
    driver.find_elements_by_xpath("//div[@id=%s]/h3/a" % 2)[0].click()
    # 拿到当前浏览器的最后一个tab,这是一个列表的形式
    tab = driver.window_handles[-1]
    # 切换到刚刚点击的页面
    driver.switch_to_window(tab)
    driver.save_screenshot('blog.png')
    # 根据标签名查到要点击的数据
    title = driver.find_element_by_tag_name("title")
    # 移动鼠标到title的位置,并且点击
    ActionChains(driver).move_to_element(title).click(title).perform()
    # 刷新页面
    driver.refresh()
    # 打印页面源码
    driver.page_source
    title = driver.find_elements_by_xpath("//a[@href='/.']")[0]
    # 移动到title下10像素位置,并且双点击
    ActionChains(driver).move_to_element_with_offset(title, 0, 10).double_click().perform()
    # 退出
    driver.quit()
  • 具体操作命令
命令 说明
click(on_element=None) 单击鼠标左键
context_click(on_element=None) 点击鼠标右键
double_click(on_element=None) 双击鼠标左键
click_and_hold(on_element=None) 点击鼠标左键,不松开
drag_and_drop(source, target) 拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) 拖拽到某个坐标然后松开
key_down(value, element=None) 按下某个键盘上的键
key_up(value, element=None) 松开某个键
move_by_offset(xoffset, yoffset) 鼠标从当前位置移动到某个坐标
move_to_element(to_element) 鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset) 移动到距某个元素(左上角坐标)多少距离的位置
perform() 执行链中的所有动作
release(on_element=None) 在某个元素位置松开鼠标左键
send_keys(*keys_to_send) 发送某个键到当前焦点的元素
send_keys_to_element(element, *keys_to_send) 发送某个键到指定元素

本文链接:时光不写博客-selenium+PhantomJS+IP代理

相关文章
|
1月前
|
Web App开发 Java 测试技术
selenium怎么使用代理IP
selenium怎么使用代理IP
59 0
|
1月前
|
Web App开发 安全 定位技术
关于使用 Python 和 Selenium chrome driver 访问 url 时修改 source ip 的问题
关于使用 Python 和 Selenium chrome driver 访问 url 时修改 source ip 的问题
60 0
|
11月前
|
Web App开发 Linux vr&ar
Linux中Chrome无界模式动态代理IP的配置(Selenium)
Linux中Chrome无界模式动态代理IP的配置(Selenium)
|
11月前
|
数据采集 JavaScript 前端开发
Selenium+代理爬取需要模拟用户交互的网站
Selenium+代理爬取需要模拟用户交互的网站
|
数据采集
selenium+chrome不关闭浏览器的情况下如何换IP?
主要分享Selenium怎样在不关闭浏览器的情况下重新设置代理IP
|
Web App开发 数据采集 测试技术
使用Selenium和代理用户名和密码在C#中进行无头浏览
Selenium是一个自动化测试工具,可以模拟用户在浏览器中的操作。在C#中使用Selenium和爬虫代理加强版IP的时候,因为代理服务器需要用户名和密码进行认证,Chrome浏览器会弹出一个认证窗口要求输入用户名和密码。可以创建一个Chrome扩展插件,然后加载使用完成自动认证窗口。
237 0
使用Selenium和代理用户名和密码在C#中进行无头浏览
【原创】selenium配置代理(账密、隧道)
【原创】selenium配置代理(账密、隧道)
【原创】selenium配置代理(账密、隧道)
selenium+chromedrive 添加代理
selenium+chromedrive 添加代理
136 0
selenium+PhantomJS
selenium+PhantomJS
64 0
|
Web App开发 前端开发 测试技术
新手教程 | Python自动化测试Selenium+chrome连接HTTP代理(账密+白名单)
虽然 Selenium 主要用于网站的前端测试,但其核心是浏览器用户代理库。本次来说说,Python使用Selenium调用Chrome浏览器并通过HTTP代理进行自动化测试

热门文章

最新文章