Python自动化学习--元素定位
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python自动化学习--元素定位,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2985字,纯文字阅读大概需要5分钟。
内容图文
![Python自动化学习--元素定位](/upload/InfoBanner/zyjiaocheng/705/f54b3aab8a2440ada34bbdc100875bcc.jpg)
from selenium import webdriver import time driver = webdriver.Chrome() driver.get("https://www.baidu.com/index.php?tn=25017023_6_pg&ssl_s=1&ssl_c=ssl1_16d7f66ac98") ''' # 元素定位之 id 定位 driver.find_element_by_id("kw").send_keys("华为") driver.find_element_by_id("su").click() ''' ''' #元素定位之 class name 定位 driver.find_element_by_class_name("s_ipt").send_keys("荣耀") driver.find_element_by_id("su").click() ''' ''' #元素定位之 tag 定位 driver.find_element_by_tag_name("input").send_keys("linux") driver.find_element_by_id("su").click() ''' ''' #元素定位之 link text 定位 driver.find_element_by_link_text("新闻").click() time.sleep(3) ''' """ #元素定位之 partial link 定位 (类似于模糊定位) driver.find_element_by_partial_link_text("hao").click() time.sleep(3) """ ''' #元素定位之 XPath 定位 绝对路径定位 driver.find_element_by_xpath('//*[@id="kw"]').send_keys("国庆盛典") driver.find_element_by_xpath('//*[@id="su"]').click() time.sleep(3) ''' ''' #元素定位之 XPath 定位 元素属性定位 driver.find_element_by_xpath('//input[@id="kw"]').send_keys("小米官网") driver.find_element_by_xpath('//input[@id="su"]').click() time.sleep(3) ''' ''' #元素定位之 XPath 元素属性与层级结合定位 driver.find_element_by_xpath('//span[@class="bg s_ipt_wr quickdelete-wrap"]/input').send_keys("裤子") time.sleep(2) ''' ''' #元素定位之 XPath 逻辑运算符定位 driver.find_element_by_xpath("//*[@id='kw' and @class='s_ipt']").send_keys("裤子") time.sleep(2) ''' ''' #元素定位之 XPath contains 方法 driver.find_element_by_xpath("//span[contains(@class,'s_ipt_wr')]/input").send_keys("裤子") time.sleep(2) ''' ''' #元素定位之 XPath text() 和 contains 配合使用,实现link text 定位的效果 driver.find_element_by_xpath("//a[contains(text(),'hao')]").click() time.sleep(2) ''' ''' #CSS 定位 通过 class 选择器定位 写法:.class driver.find_element_by_css_selector(".s_ipt").send_keys("裤子") time.sleep(2) ''' ''' #CSS 定位 通过 id 选择器定位 写法:#id driver.find_element_by_css_selector("#kw").send_keys("裤子") time.sleep(2) ''' ''' #CSS 定位 通过标签层级关系定位 写法:element > element driver.find_element_by_css_selector("span > input").send_keys("裤子") time.sleep(2) ''' ''' #CSS 定位 通过属性定位 #driver.find_element_by_css_selector("[autocomplete=off]").send_keys('裤子') driver.find_element_by_css_selector("[name='wd']").send_keys('裤子') driver.find_element_by_css_selector("[type='submit']").click() time.sleep(2) ''' ''' #CSS 定位 组合定位 driver.find_element_by_css_selector("form.fm > span > input.s_ipt").send_keys("裤子") driver.find_element_by_css_selector("form#form > span > [type='submit']").click() time.sleep(2) ''' ''' #By方式的 8 种定位方式,写法差不多,只是需要将By方式导入才能使用 from selenium.webdriver.common.by import By driver.find_element(By.ID,"kw").send_keys("裤子") driver.find_element(By.NAME,"wd").send_keys("裤子") driver.find_element(By.CLASS_NAME,"s_ipt").send_keys("裤子") driver.find_element(By.TAG_NAME,"input").send_keys("裤子") driver.find_element(By.LINK_TEXT,"新闻").click() driver.find_element(By.PARTIAL_LINK_TEXT,"hao").click() driver.find_element(By.XPATH,"//*[@class='bg s_btn']") driver.find_element(By.CSS_SELECTOR,"span.bg s_btn_wr > input#su") ''' time.sleep(2) driver.quit()
内容总结
以上是互联网集市为您收集整理的Python自动化学习--元素定位全部内容,希望文章能够帮你解决Python自动化学习--元素定位所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。