zoukankan      html  css  js  c++  java
  • python之selenium

    一、安装

    1、selenium

    pip install selenium

    2、驱动

     (1)下载驱动

    https://sites.google.com/a/chromium.org/chromedriver/downloads
    
    https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
    
    https://github.com/mozilla/geckodriver/releases
    
    https://webkit.org/blog/6900/webdriver-support-in-safari-10/
    

    (2)配置环境

      下载好对应版本驱动,配置环境PATH(Windows),PATH加入驱动路径

    二、简单实用

    1、执行js,打开浏览器窗口

    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get("http://8.8.8.8/")
    
    
    for i in range(100):
    	js="window.open('http://1.1.1.%s')" % str(i)
    	driver.execute_script(js)
    
    #driver.quit()
    

      

    2、简单使用2

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome()
    driver.get("http://www.python.org")
    assert "Python" in driver.title
    elem = driver.find_element_by_name("q")
    elem.clear()	#清空自动填入的值,保证我们的输入
    elem.send_keys("pycon")
    elem.send_keys(Keys.RETURN)
    assert "No results found." not in driver.page_source
    driver.close()	#退出
    

      

    3、获取元素

    1.id定位:find_element_by_id(self, id_)
    2.name定位:find_element_by_name(self, name)
    3.class定位:find_element_by_class_name(self, name)
    4.tag定位:find_element_by_tag_name(self, name)
    5.link定位:find_element_by_link_text(self, link_text)
    6.partial_link定位find_element_by_partial_link_text(self, link_text)
    7.xpath定位:find_element_by_xpath(self, xpath)
    8.css定位:find_element_by_css_selector(self, css_selector)
    9.id复数定位find_elements_by_id(self, id_)
    10.name复数定位find_elements_by_name(self, name)
    11.class复数定位find_elements_by_class_name(self, name)
    12.tag复数定位find_elements_by_tag_name(self, name)
    13.link复数定位find_elements_by_link_text(self, text)
    14.partial_link复数定位find_elements_by_partial_link_text(self, link_text)
    15.xpath复数定位find_elements_by_xpath(self, xpath)
    16.css复数定位find_elements_by_css_selector(self, css_selector
    

      

    refer:https://www.cnblogs.com/yoyoketang/p/6557421.html

    https://selenium-python.readthedocs.io/locating-elements.html

    4、一些方法和其他

    (1)Selenium:利用select模块处理下拉框

    from selenium.webdriver.support.select import Select

    select_by_index # 通过索引定位

    select_by_value # 通过value值定位

    select_by_visible_text # 通过文本值定位

    根据索引选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_index("3")

    根据value值选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_value("3线")

    根据文本值选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_visible_text("3线")

    refer:

    (2)selenium中sendkeys()方法输入中文报错之解决方案

    #方式1,在中文前加入u 
    # driver.find_element_by_class_name(“s_ipt”).send_keys(u’测试’)
    
    #方式2,使用decode()方法 
    str = ‘测试’ 
    print str 
    driver.find_element_by_class_name(“s_ipt”).send_keys(str.decode(‘utf-8’)) 

    (3)获取当前页面信息

    记得延时,不然有可能出错

    driver.title  #标题

    driver.current_url  #链接

    (4)输入

    element.send_keys("some text")

    (5)点击

    driver.find_element_by_id("submit").click()
    

      

    (6)前进后退

    driver.forward()
    driver.back()
    

      

    (7)Cookies

    cookie = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
    driver.add_cookie(cookie)
    driver.get_cookies()

    5、等待

    https://selenium-python.readthedocs.io/waits.html

    6、api

    https://selenium-python.readthedocs.io/api.html

    https://seleniumhq.github.io/selenium/docs/api/py/api.html

  • 相关阅读:
    博客
    Windows Live Writer
    VirtualBox
    Linux dd
    Nginx与tomcat组合的简单使用
    压力测试之badboy和Jmeter的简单使用方法
    WebGIS中基于控制点库进行SHP数据坐标转换的一种查询优化策略
    浅谈利用SQLite存储离散瓦片的思路和实现方法
    常见ArcGIS操作(以10.0为例)
    (二十一)WebGIS中鹰眼的实现思路
  • 原文地址:https://www.cnblogs.com/nobgr/p/10579602.html
Copyright © 2011-2022 走看看