zoukankan      html  css  js  c++  java
  • Selenium+Python浏览器调用:Firefox

    python -m pydoc -p  4567

    说明:

    python -m pydoc表示打开pydoc模块,pydoc是查看python文档的首选工具;

    -p 4567表示在4567端口上启动server

    然后在浏览器中访问http://localhost:4567/,此时应该可以看到python中所有的Modules按ctrl+f,输入selenium,定位到selenium文档的链接,然后点击进入到http://localhost:4567/selenium.html这个页面

    这就是selenium文档所在的位置了,接下来便可以根据自己的需要进行查看了。举个例子,如果你想查看Webdriver类的基本方法,可以访问这个页面http://localhost:4567/selenium.webdriver.remote.webdriver.html

    Firefox浏览器调用

    Firefox浏览器驱动添加

    Firefox原生支持,无需下载驱动,只要安装浏览器即可

    Firefox浏览器的调用

    代码如下:

    #coding=utf-8
    
    from selenium import webdriver
    
    driver=webdriver.Firefox()
    
    url='http://www.baidu.com'
    
    driver.get(url)
    
    driver.close()

    说明:

    1、【#coding=utf-8】为了防止乱码问题,以便在程序中添加中文注释,把编码统一为UTF-8,注意=两遍不要留空格,否则不起作用,另外【#_*_coding:utf-8_*_】的写法也可以达到相同的作用

    2、【from selenium import webdriver】该步骤是导入selenium的webdriver包,只有导入selenium包,我们才能使用webdriver API进行自动化脚本的开发

    3、【driver=webdriver.Firefox()】这里将控制webdriver的Firefox赋值给driver,通过driver获得浏览器操作对象,后就可以启动浏览器、打开网址、操作对应的页面元素了。

    python + selenium 打开百度,并在百度搜索里搜索Python

    代码如下:

    #coding=utf-8 
    import time from selenium 
    import webdriver
    driver = webdriver.Firefox()
    url = "https://www.baidu.com/" 
    try:
         driver.get(url)
         time.sleep(2)
         try:
             driver.find_element_by_css_selector("#kw").clear()
             driver.find_element_by_css_selector("#kw").send_keys("python")
             driver.find_element_by_css_selector("#su").click()
             print("搜索成功")
         except:
             print("搜索失败")
             driver.close()
         time.sleep(2)
         driver.close()
     except:
         print("网页打开失败")

    这个脚本是根据css元素定位百度搜索的输入框和按钮,来进行对网页的操作。

  • 相关阅读:
    Thinkphp回顾(五)之前台模板中的基本语法
    Thinkphp回顾之(四)查询方法深入学习
    Thinkphp框架回顾(三)之怎么实现平常的sql操作数据库
    Thinkphp学习回顾(二)之config.php的配置
    Thinkphp学习回顾(一)之基本结构目录
    端口
    curl put delete post get请求类型参数
    xshell连接virtualbox下的linux系统
    实现jsonp的三种方式
    匹配汉字
  • 原文地址:https://www.cnblogs.com/APeng2019/p/10719369.html
Copyright © 2011-2022 走看看