导包:from selenium.webdriver.common.by import By
类型:id、name、class_name、elements、xpath、css_selector、tag_name、link_text、partial_link_text
find_element
一、By.ID
<input type="text" autofocus="autofocus" class="text_cmu" value="" placeholder="手机号/邮箱" name="username" id="username" autocomplete="off">
driver.find_element(By.ID, "username")
二、By.NAME
<input type="text" autofocus="autofocus" class="text_cmu" value="" placeholder="手机号/邮箱" name="username1" id="username" autocomplete="off">
driver.find_element(By.NAME, "username1")
三、By.CLASS_NAME
<input type="text" autofocus="autofocus" class="text_cmu" value="" placeholder="手机号/邮箱" name="username1" id="username" autocomplete="off">
driver.find_element(By.CLASS_NAME, "text_cmu")
注意点:
1.class属性的值可能存在多个,通常情况下是通过空格进行分隔,在定位时只能选择其中一个属性值;
2.挑选属性值时尽量挑选能唯一表示该元素属性值。或者说是该元素class属性所特有的值。
四、By.TAG_NAME
<input type="text" autofocus="autofocus" class="text_cmu" value="" placeholder="手机号/邮箱" name="username1" id="username" autocomplete="off">
# 标签定位
driver.find_element(By.TAG_NAME, "input")
五、By.LINK_TEXT
<div class="links-wrap-h clearfix">
<a href="https://www.taobao.com">淘宝网</a>
<a href="https://www.jd.com">京东</a>
<a href="https://www.vip.com">唯品会</a>
<a href="https://www.suning.com" target="_blank">苏宁易购</a>
<a href="https://www.baidu.com">百度</a>
<a href="http://www.yhd.com">1号店</a>
</div>
driver.find_element(By.LINK_TEXT, "百度")
# 注意点:
# 1、对应的参数必须为元素的全部文本内容,就算后面有空格也需要一起拷贝
六、By.PARTIAL_LINK_TEXT
<div class="links-wrap-h clearfix">
<a href="https://www.taobao.com">淘宝网</a>
<a href="https://www.jd.com">京东</a>
<a href="https://www.vip.com">唯品会</a>
<a href="https://www.suning.com" target="_blank">苏宁易购</a>
<a href="https://www.baidu.com">百度</a>
<a href="http://www.yhd.com">1号店</a>
</div>
driver.find_element(By.PARTIAL_LINK_TEXT, "苏宁易").click()
注意点:
1.在挑选局部文本内容时选择能唯一表示该元素文本信息
2.局部:文本字符串中的一串连续的字符信息,必须是连续不是间断
七、By.XPATH
绝对路径
driver.find_element(By.XPATH, "/html/body/div[2]/div/div[2]/div/form/div/div[5]/div[2]")
相对路径
driver.find_element(By.XPATH, "//form/div/div[5]/div[1]")
属性定位
<form id="loginform" method="post">
<div class="layel2">
<div class="text_uspa">
<label class="judgp uspa_user"></label>
<input type="text" autofocus="autofocus" class="text_cmu" value="" placeholder="手机号/邮箱" name="username" id="username" autocomplete="off">
</div>
<div class="text_uspa">
<label class="judgp uspa_pwd"></label>
<input type="password" class="text_cmu" value="" placeholder="密码" name="password" id="password" autocomplete="off">
</div>
<div class="text_uspa check_cum">
<input type="text" class="text_cmu" value="" placeholder="验证码" name="verify_code" id="verify_code" autocomplete="off">
</div>
<div class="check_cum_img">
<img src="/index.php?m=Home&c=User&a=verify" id="verify_code_img" onclick="verify()">
</div>
<div class="sum_reme_for p">
<div class="autplog">
</div>
<div class="foget_pwt">
<span style="color:#F00;">测试号:13800138006 密码 123456</span>
<a href="/index.php/Home/User/forget_pwd.html">忘记密码?</a>
</div>
</div>
<div class="login_bnt">
<a href="javascript:void(0);" onclick="checkSubmit();" class="J-login-submit" name="sbtbutton">登 录</a>
</div>
</div>
</form>
# 属性+逻辑+层级 定位验证码
driver.find_element(By.XPATH, "//*[@id='verify_code']")
driver.find_element(By.XPATH, "//*[@placeholder='验证码']")
driver.find_element(By.XPATH, "//*[@placeholder='验证码' and @class='text_cmu']")
driver.find_element(By.XPATH, "//*[@id='loginform']/div/div[3]/input")
driver.find_element(By.XPATH, "//*[@id='loginform']/div/div[3]/input[@placeholder='验证码' and @class='text_cmu']")
# 文本 定位手机号
通过元素的局部文本信息来进行定位
driver.find_element(By.XPATH, "//*[contains(@placeholder, '机号/邮')]")
通过元素的开始属性值定位
driver.find_element(By.XPATH, "//*[starts-with(@placeholder, '手机')]")
八、By.CSS_SELECTOR
<div class="text_uspa check_cum">
<input type="text" class="text_cmu" value="" placeholder="验证码" name="verify_code" id="verify_code" autocomplete="off">
</div>
id选择器
#(如:verify_code,就要写成‘#verify_code’)
driver.find_element(By.CSS_SELECTOR, "#verify_code")
class选择器
.(如:text_cmu,就要写成‘.text_cmu’)
driver.find_element(By.CSS_SELECTOR, ".text_cmu ")
元素选择器
# 属性选择器
# [属性名='属性值'] 或者 标签名[属性名='属性值']
driver.find_element(By.CSS_SELECTOR, "[placeholder='验证码']")
driver.find_element(By.CSS_SELECTOR, "input[placeholder='验证码']")
# 如果想要多个属性值来进行定位,css属性选择器的写法如下
# [属性名1='属性值1'][属性名='属性值2']
driver.find_element(By.CSS_SELECTOR, "[placeholder='验证码'][id='verify_code']")
# 部分属性定位
# [属性名*='局部的属性值']
# 标签名[属性名*='局部的属性值']
driver.find_element(By.CSS_SELECTOR, "[placeholder*='验证']")
# [属性名^='属性值的开头']
# 标签名[属性名^='属性值的开头']
driver.find_element(By.CSS_SELECTOR, "[placeholder^='验']")
# [属性名$='属性值的结尾']
# 标签名[属性名$='属性值的结尾']
driver.find_element(By.CSS_SELECTOR, "[placeholder$='码']")
# 注意:局部的属性值是能唯一表示该元素一部分值
层级选择器
driver.find_element(By.CSS_SELECTOR, ".check_cum>input")
driver.find_element(By.CSS_SELECTOR, ".check_cum input")
find_elements
# 获取所有的input标签
a = driver.find_elements(By.TAG_NAME, "input")
# 通过索引操作
a[2].send_keys("1234")
# 注意点:
1.获取一组元素返回的来的数据是一个列表(数组)
2.需要取具体某一个要操作的元素对象时,只需要对返回的列表加上下标即可
3.下标起始值为0,也就是列表的索引值