selenium的实际使用

1.标签页的切换

#获取当前所有的窗口

cur=driver.window_handles

#根据窗口索引进行切换

driver.switch_to.window(cur[1])

复制代码
from selenium import webdriver

import time

driver = webdriver.Chrome()

driver.get('http://www.baidu.com')

time.sleep(1)

ele=driver.find_element_by_id('kw')

ele.send_keys('九歌')

time.sleep(2)





driver.find_element_by_id('su').click()

time.sleep(2)



#通过执行js来新开一个标签页

js='window.open("https://www.sougou.com")'

driver.execute_script(js)

time.sleep(1)

#获取所有的窗口

windows=driver.window_handles

#根据索引进行切换

driver.switch_to.window(window[0])

time.sleep(2)

driver.switch_to.window(window[1])

time.sleep(2)

driver.quit()

#2.页面滚动

复制代码
driver = webdriver.Chrome()

driver.get('http://www.baidu.com')

time.sleep(1)

ele=driver.find_element_by_id('kw')

ele.send_keys('九歌')

time.sleep(1)

driver.find_element_by_id('su').click()



time.sleep(2)

//滚动到底部

#js='document.documentElement.scrollTop=8000'

//滚动部分

js='window.scrollTop(0,700)' # 700像素

driver.execute_script(js)

time.sleep(2)

driver.quit()

#3.京东图书

url=' '

#价格://*[@id="]_goodsLists"]/ul/li[*]/div/div[2]/strong/i

#标题://div[@class="p-name"]/a/em/text()

driver=webdriver.Chrome()

driver.get(url)

time.sleep(2)

data=driver.page_source

html=etree.HTML(data)

#标题

title=html.xpath('//div[@class="p-name"]/a/em/text()')

print(title)

#价格

price=html.xpath('//*[@id="]_goodsLists"]/ul/li[*]/div/div[2]/strong/i')

print(price)

driver.quit()

n=0

for t in title:

dic={}

dic['title']=t

dic['price']=price[n]

n+=1

print(dic)

相关推荐
黑猫学长呀13 小时前
存储宝典第6篇:测试机台的PE板和PPB板有什么区别?
测试工具·fpga开发·ssd·芯片测试·ate·存储芯片·测试机台
测试修炼手册17 小时前
[测试工具] Codex Skill实战:把测试经验沉淀成可复用能力
运维·服务器·测试工具
小卓(friendhan2005)18 小时前
基于 Pytest + Selenium + Allure 的博客系统自动化测试实践
selenium·测试工具·pytest
软件测试慧姐2 天前
软件测试面试题总结【含答案】
软件测试·测试工具·面试
菜_小_白2 天前
tcpdump
linux·网络·测试工具·http·tcpdump
littlebigbar2 天前
亲身体验AI智能体在实际项目中展现的核心能力
人工智能·selenium·测试工具
测试员周周2 天前
【Appium 系列】第09节-数据驱动测试 — YAML 数据 + parametrize
服务器·数据库·人工智能·python·测试工具·语言模型·appium
阿斯加德D2 天前
我的世界生活大冒险整合包下载高版本2026最新分享
测试工具·游戏·游戏程序·生活·材质
Land03292 天前
指纹浏览器自动化集成方案|多浏览器RPA适配实战记录
运维·人工智能·爬虫·python·selenium·自动化·rpa
littlebigbar2 天前
让AI自己说说,AI 智能体在软件测试中能做什么?
人工智能·测试工具