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")
  • 相关阅读:
    tomcat8.5配置redis实现session共享(tomcat-redis-session-manager-master)
    HDU4292(KB11-H 最大流)
    HDU4280(KB11-G 最大流)
    POJ1149(最大流)
    POJ3281(KB11-B 最大流)
    POJ3436(KB11-A 最大流)
    Codeforces841B
    Codeforces841A
    HDU2255(KB10-K 二分图最大权匹配)
    HDU1281(KB10-D 二分图最大匹配)
  • 原文地址:https://www.cnblogs.com/jialeliu/p/13705774.html
Copyright © 2011-2022 走看看