selenium使用find_element定位元素方法
from selenium import webdriver
import time
driver = webdriver.Chrome()
# driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
# <input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">
# <a href="http://news.baidu.com" target="_blank" class="mnav">新闻</a>
# driver.find_element_by_id("kw").send_keys("python")
# driver.find_element_by_name("wd").send_keys("python")
# driver.find_element_by_class_name("s_ipt").send_keys("虫师自动化")
# driver.find_element_by_tag_name("input")
# driver.find_element_by_link_text("新闻").click() #输入需要定位的链接全部文字
# driver.find_element_by_partial_link_text("hao").click() #输入需要定位的链接部分文字
# Xpath绝对路径定位
# driver.find_element_by_xpath("/html/body/div/div/div/div/div/from").send_keys("hello")
# 使用单斜线表示当前路径,双斜线表示不确定路径
# driver.find_element_by_xpath("//*[@id='kw']").send_keys("python") #id属性
# driver.find_element_by_xpath("//*[@name='wd']").send_keys("selenium") # name属性
# driver.find_element_by_xpath("//input[@maxlength='100']").send_keys("传智播客")
# Xpath可以使用and or来进行多个属性同时定位
driver.find_element_by_xpath("//*[@id='kw' and @name='wd']").send_keys('hello world')
# //*[@id="kw"]
# 定位上级
# driver.find_element_by_xpath("//form[@id='form']/span/input").send_keys("软件测试")
# driver.find_element_by_xpath("//div[@class='s_form_wrapper soutu-env-nomac soutu-env-newindex']/form/span/input").send_keys("虾无忧")
# CSS定位元素
#kw
# html.sui-componentWrap body.s-manhattan-index div#wrapper div#head div#head_wrapper.s-isindex-wrap.head_wrapper.s-title-img div#s_fm.s_form div#s_form_wrapper.s_form_wrapper.soutu-env-nomac.soutu-env-newindex form#form.fm span#s_kw_wrap.bg.s_ipt_wr.quickdelete-wrap input#kw.s_ipt
driver.find_element_by_css_selector(".s_ipt").send_keys("hello") # .表示class
driver.find_element_by_css_selector("#kw").send_keys("nihao") # #表示ID
driver.find_element_by_css_selector("input").send_keys("Python") # input标签名称
driver.find_element_by_css_selector("span > input").send_keys("python ") # 表示span标签下面的子标签input
driver.find_element_by_css_selector("form#form > span > input").send_keys("python") # 表示form标签下面与id元素form,form表单下面的span标签,span标签下面的input
driver.find_element_by_css_selector("input[maxlength='100']").send_keys('python') # 表示任意一种元素和属性
time.sleep(6)
driver.close()