zoukankan      html  css  js  c++  java
  • Selenium+python --CSS定位方法

    跟着悠悠学
    # coding:utf-8
    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get("https://www.baidu.com")
    # <input id="kw" class="s_ipt" type="text" autocomplete="off" maxlength="100" name="wd"/>
    # css 通过id属性定位
    driver.find_element_by_css_selector("#kw").send_keys("python")
    # css通过class属性定位
    driver.find_element_by_css_selector(".s_ipt").send_keys("python")
    # css 通过标签属性定位
    diver.find_element_by_css_selector("input").send_keys("python")

    # css by name
    driver.find_element_by_css_selector("[name='wd']").send_keys("python")
    # css by autocomplete
    driver.find_element_by_css_selector("[autocomplete='of']").send_keys("python")
    # css by type
    driver.find_element_by_css_selector("[type='text']").send_keys("python")

    # css通过标签与属性组合来定位元素
    driver.find_element_by_css_selector("input:contains('kw')")
    # css通过标签与class属性组合
    driver.find_element_by_css_selector("input.s_ipt").send_keys("python")
    # css通过标签与id属性的组合
    driver.find_element_by_css_selector("input#kw").send_keys("python")
    # css通过标签与其它属性组合
    driver.find_element_by_css_selector("input[id='kw']").send_keys("python")

    # css通过层级关系定位
    driver.find_element_by_css_selector("form#form>span>input").send_keys("python")
    # css通过层级关系定位
    driver.find_element_by_css_selector("form.fm>span>input").send_keys("python")


    # 通过索引option:nth-child(1)来定位元素
    # 选择第1个option
    driver.find_element_by_css_selector("select#nr>option:nth-child(1)").click()
    # 选择第2个option
    driver.find_element_by_css_selector("select#nr>option:nth-child(2)").click()
    # 选择第3个option
    driver.find_element_by_css_selector("select#nr>option:nth-child(3)").click()

    # css:逻辑运算
    driver.find_element_by_css_selector("input[id='kw'][name='wd']").send_keys("python")
  • 相关阅读:
    LeetCode 18. 4Sum (四数之和)
    开运算、闭运算、形态学梯度、顶帽、黑帽
    膨胀和腐蚀
    四种滤波方式
    关于平滑处理及图像滤波与滤波器
    27、简述redis的有哪几种持久化策略及比较?
    26、redis中默认有多少个哈希槽?
    25、如何实现redis集群?
    大聊Python----Select解析
    大聊Python----IO口多路复用
  • 原文地址:https://www.cnblogs.com/shanliguniang/p/10637573.html
Copyright © 2011-2022 走看看