微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python(81)_selenium定位页面元素

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()

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐