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)

相关推荐
se-tester8 天前
JMeter、Postman 和 SoapUI 在做接口测试上的优势和缺点
测试工具·jmeter·接口测试·postman·soapui
xixi09248 天前
selenium IDE——command
selenium·测试工具
xixi09248 天前
selenium IDE安装使用教程
ide·selenium·测试工具
Wpa.wk9 天前
har文件转为接口自动化测试用例
运维·测试工具·自动化·测试用例·接口自动化
Wpa.wk9 天前
ubuntu22桌面版安装
经验分享·测试工具
uyeonashi9 天前
自动化测试问卷考试系统测试报告
功能测试·selenium·github
007张三丰9 天前
软件测试专栏(6/20):Selenium从0到1实战指南:元素定位、等待机制与框架封装
selenium·测试工具
测试老哥9 天前
如何使用Postman做接口测试?
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
安全不再安全9 天前
某驱动任意读漏洞分析 - 可用于游戏内存数据读取
c语言·测试工具·安全·游戏·网络安全