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

  • 相关阅读:
    [Leetcode]Linked List Cycle
    [Leetcode]Excel Sheet Column Number
    [Leetcode]Unique Binary Search Trees
    [Leetcode]Same Tree
    同时访问内外网设置路由信息
    希腊字母表示及读音
    jni入门
    查看某个进程运行时间的几种方法
    企业级hbase HA配置
    存在单点故障的namenode宕机恢复测试
  • 原文地址:https://www.cnblogs.com/sherlock-merlin/p/9482071.html
Copyright © 2011-2022 走看看