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)

相关推荐
愈努力俞幸运2 天前
python selenium 用法教程
python·selenium
Future_yzx2 天前
Selenium 浏览器操作与使用技巧——详细解析(Java版)
java·selenium·测试工具
Algorithm-0073 天前
【Postman 接口测试】接口用例设计与评审
测试工具·postman
武陵悭臾3 天前
网络爬虫学习:应用selenium获取Edge浏览器版本号,自动下载对应版本msedgedriver,确保Edge浏览器顺利打开。
学习·selenium·edge·deepseek·winreg·zipfile
Future_yzx3 天前
selenium自动化测试框架——面试题整理
java·selenium·intellij-idea
hunter2062064 天前
ubuntu无法上网的解决办法
网络·测试工具·ubuntu
代码的乐趣4 天前
支持selenium的chrome driver更新到132.0.6834.110
chrome·python·selenium
霍格沃兹测试开发学社测试人社区4 天前
性能测试丨JVM 性能数据采集
软件测试·jvm·测试开发·测试工具
Algorithm-0074 天前
【Postman接口测试】Postman的全局变量和环境变量设置
测试工具·postman
安冬的码畜日常4 天前
【玩转 Postman 接口测试与开发2_009】第八章:利用 Postman 的 Flows 模块实现工作流测试(Workflow Testing)
测试工具·接口测试·postman·workflow·api接口·postman flows·工作流测试