1、通过id来定位
#-*-coding:utf-8-*-
from selenium import webdriver
f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
f.get("http://www.baidu.com")
f.find_element_by_id('kw').send_keys('selenium')
f.find_element_by_id('su').click()
2、通过name来定位
<input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">
#-*-coding:utf-8-*-
from selenium import webdriver
f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
f.get("http://www.baidu.com")
f.find_element_by_name('wd').send_keys('selenium')
f.find_element_by_id('su').click()
f.quit()
3、通过class来定位
#-*-coding:utf-8-*-
from selenium import webdriver
f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
f.get("http://www.baidu.com")
f.find_element_by_class_name('s_ipt').send_keys('selenium')
f.find_element_by_id('su').click()
f.quit()
要查找的都是要唯一的
4、通过tag_name来定位
标签名 input标签
一般不唯一
#-*-coding:utf-8-*-
from selenium import webdriver
f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
f.get("http://www.baidu.com")
inputs = f.find_elements_by_tag_name('input')
for i in inputs:
if i.get_attribute('autocomplete') == 'off':
i.send_keys('selium')
f.find_element_by_id('su').click()
f.quit()
for循环勾选所有复选框
5、通过link_text定位
#-*-coding:utf-8-*-
from selenium import webdriver
f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
f.get("http://www.baidu.com")
f.find_element_by_link_text("新闻").click()
f.quit()
6、通过partial_link_text定位
#-*-coding:utf-8-*-
from selenium import webdriver
f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
f.get("http://www.baidu.com")
f.find_element_by_partial_link_text("123").click()
f.quit()