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

    1、下载chromedriver文件

    http://chromedriver.storage.googleapis.com/index.html
    google官方下载地址

    http://dl.nwjs.io/
    推荐下载nwjs sdk,chromedriver文件包含在sdk中,下载后解压即可找到


    2、代码示例

    import time
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    ops = Options()
    ops.add_argument("nwapp=myapp")    #myapp可以是一个文件夹!你的应用文件夹和nw.exe在同一个目录下即可
    
    driver = webdriver.Chrome(ops)   
    driver.maximize_window()          #最大化窗口,非必须
    
    driver.find_element_by_id("username").send_keys("admin")
    driver.find_element_by_id("password").send_keys("admin")
    driver.find_element_by_xpath("//*[@id='login']/form/p/input").click()
    time.sleep(1)
    driver.quit()

    3、常用选择器

    1.id定位:find_element_by_id("id")
    2.name定位:find_element_by_name("name")
    3.class定位:find_element_by_class_name("classname")
    4.tag定位:find_element_by_tag_name("tagname")
    5.link_text定位:find_element_by_link_text("linktext")
    6.partial_link定位find_element_by_partial_link_text("partial_link")
    7.xpath定位:find_element_by_xpath("xpath")
    8.css定位:find_element_by_css_selector("css")

     有的元素比较难定位,这时候用xpath是比较方便的,chrome可以自动生成xpath,只需要在chrome控制台选中相关元素,右键->Copy->copy xpath即可得到xpath。如下图所示

     4、使用WebDriverWait来设置延时

    time.sleep()虽然可以用来设置延时,但是其比较死板,只能设置指定的时间,面对不稳定的网络(有时加载快,有时加载慢),WebDriberWait显得更合适些,它是在指定时间内找到相关元素便可进行下一步操作。

    想要使用WebDriverWait需要引入两个模块:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC

    用法参考:

      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="upload-tips"]/div[2]'))).click()

    写起来会麻烦一点

    参考链接:
    https://www.jianshu.com/p/56f2ce87b1f4
    https://blog.csdn.net/zeping891103/article/details/50790180

  • 相关阅读:
    动态表单实现客户端二次过滤及字段汇总统计
    开放一些常见功能的工具类代码
    动态表单
    客户中增加按钮提前判断是否撞单 并提示
    通过插件来对打印数据进行处理
    mac 升级10.12 php debug 环境 跑不起的解决 解决方案
    感觉世界变化太快...
    Mac 升级一次,php 就崩溃一次,有味,苹果....
    http://s22.app1105796624.qqopenapp.com/
    unity 2d 游戏优化之路 遇坑记录
  • 原文地址:https://www.cnblogs.com/sherlock-merlin/p/9482071.html
Copyright © 2011-2022 走看看