web自动化(二)元素定位 xpath定位&css定位

常用的元素定位:ID,Name,xpath,css 等等

selenuim demo

bash 复制代码
import time

from selenium import  webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import  WebDriverWait
from selenium.webdriver.support import  expected_conditions as EC
web_driver=webdriver.Firefox()
url="https://www.baidu.com"
web_driver.get(url)
#最多等待5秒,如果在2秒找到了元素 ,会执行下一步
# web_driver.implicitly_wait(5)

#点击 新闻 连接
el=(By.LINK_TEXT,"新闻")
el_select=web_driver.find_element(*el)
#等待元素 最长等待10秒,每2秒找一次
WebDriverWait(web_driver,10,2).until(EC.presence_of_element_located(el))
el_select.click()


time.sleep(3)
#切回第1个tab
web_driver.switch_to.window(web_driver.window_handles[0])
#搜索selenuim
web_driver.find_element(By.ID,"kw").send_keys("selenuim")
web_driver.find_element(By.ID,"su").click()
time.sleep(3)

web_driver.quit()

DrissionPage demo

帮助文档http://drissionpage.cn

bash 复制代码
import time
from DrissionPage import WebPage
from  DrissionPage.common import By as DrissionBy
web_page=WebPage('d')
# get()已内置等待加载开始,后无须跟wait.load_start()
web_page.get(url)


#XPATH 获取元素
# loc=(DrissionBy.XPATH,'//*[@id="s-top-left"]/a[1]')
#link_text 获取元素
loc=(DrissionBy.LINK_TEXT,'新闻')
# 等待元素加载 超时时间为5秒
web_page.wait.eles_loaded(loc,timeout=5)
web_page.ele(loc).click()

time.sleep(3)

# DrissionPage 3.x 开始就支持多 tab 对象共存,对象之间互不影响,而且标签页无需激活即可操作。因此不再需要切换标签页。
loc=(DrissionBy.ID,"kw")
#设置文本框值
web_page.ele(loc).input("DrissionPage")
loc=(DrissionBy.ID,"su")
web_page.ele(loc).click()


time.sleep(10)
web_page.quit()
相关推荐
资讯分享周3 小时前
破局遗留系统!AI自动化重构:从静态方法到Spring Bean注入实战
spring·重构·自动化
雨中夜归人3 小时前
自动化测试工具playwright中文文档-------14.Chrome 插件
python·测试工具·自动化·pytest·playwright
越学不动啦4 小时前
十、自动化函数+实战
运维·软件测试·自动化·测试
海姐软件测试1 天前
面试求助:在性能测试中发现CPU占用过高应该如何进行分析?
面试·自动化
来自于狂人1 天前
当Browser Use遇见A2A:浏览器自动化与智能体协作的“冰与火之歌“
运维·自动化
老秦包你会1 天前
个人博客测试报告
自动化
MaisieKim_1 天前
IT运维常用的软件工具有哪些
运维·自动化
TOWNST1 天前
Python Selenium 一小时速通教程
开发语言·python·selenium
晓13131 天前
第三章 爬虫提速、selenium模块、requests模块进阶(终)
爬虫·python·selenium·测试工具·http
越学不动啦1 天前
八、自动化函数
运维·软件测试·自动化·dubbo·测试