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")
  • 相关阅读:
    汉字的几何中心
    输入带空格的string类型字符串 c++
    cin函数返回值
    win7玩游戏两边有黑条
    unresolved external symbol __imp__WSACleanup@0
    sizeof和strlen
    printf 函数返回值
    clone() 操作系统实验
    unsigned char 与 char
    【转】向字符数组输入空格的方法
  • 原文地址:https://www.cnblogs.com/jialeliu/p/13705774.html
Copyright © 2011-2022 走看看