zoukankan      html  css  js  c++  java
  • Python+Selenium之CSS元素定位(以百度为例)

     1 from selenium import webdriver
     2 driver = webdriver.Chrome()
     3 driver.get("https://www.baidu.com/")
     4 #  通过id属性(css属性)
     5 driver.find_element_by_css_selector("#kw").send_keys("python")
     6 #  通过class属性定位(css属性)
     7 driver.find_element_by_css_selector(".s_ipt").send_keys("python")
     8 #  通过标签定位(css属性)
     9 driver.find_element_by_css_selector("input").send_keys("python")
    10 #  通过name属性定位(其他属性)
    11 driver.find_element_by_css_selector("[name='wd']").send_keys("python")
    12 #  通过autocomplete属性定位(其他属性)
    13 driver.find_element_by_css_selector("[autocomplete='off']").send_keys("python")
    14 #  通过type属性定位(其他属性)
    15 driver.find_element_by_css_selector("[type='text']").send_keys("python")
    16 #  css也可以通过标签与属性的组合来定位元素
    17 driver.find_element_by_css_selector("input.s_ipt").send_keys("python")
    18 driver.find_element_by_css_selector("input#kw").send_keys("python")
    19 driver.find_element_by_css_selector("input[id='kw']").send_keys("python")
    20 #  css层级关系
    21 driver.find_element_by_css_selector("form#form>span>input").send_keys("python")
    22 driver.find_element_by_css_selector("form.fm>span>inout").send_keys("python")
    23 #  css索引
    24 driver.find_element_by_css_selector("select#nr>option:nth-child(1)").click
    25 driver.find_element_by_css_selector("select#nr>option:nth-child(2)").click
    26 driver.find_element_by_css_selector("select#nr>option:nth-child(3)").click
    27 #  css逻辑运算
    28 driver.find_element_by_css_selector("input[id='kw'][name='wd']").send_keys("python")
  • 相关阅读:
    CAP原理、一致性模型、BASE理论和ACID特性
    MyBatis双数据源配置
    MySQL中间件Atlas安装及使用
    MySQL主从切换
    MySQL定时逻辑备份
    MySQL主从搭建
    zabbix监控nginx
    SVN Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。
    ie8下table的colspan属性与max-with属性的显示错乱问题
    MVC 自定义异常错误页面处理
  • 原文地址:https://www.cnblogs.com/jialeliu/p/13705774.html
Copyright © 2011-2022 走看看