selenium的实际使用

1.标签页的切换

#获取当前所有的窗口

cur=driver.window_handles

#根据窗口索引进行切换

driver.switch_to.window(cur1)

复制代码
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/div2/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/div2/strong/i')

print(price)

driver.quit()

n=0

for t in title:

dic={}

dic'title'=t

dic'price'=pricen

n+=1

print(dic)

相关推荐
JZMSYYQ17 小时前
电输运性质测试系统主要测试哪些方面
测试工具·材料测试·电性能·电性能测试·电输运测试·测试设备
中国软件测试质量协会19 小时前
国产UI自动化测试工具的“技术代际“演进与自主可控之路
测试工具·ui·自动化
zzzzzzzz'1 天前
浅谈自动化测试2——selenium库中的常用函数
selenium·测试工具·文件上传·导航栏·弹窗·定位元素·查找元素
蒲公英内测分发2 天前
AI 玩具 App 每周更新,怎么用 CI/CD 自动上传测试包又避免误发?
人工智能·测试工具·智能硬件·web app
程序员小远2 天前
接口测试知识总结
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
测试秃头怪2 天前
Postman中变量的使用
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
破土士V2 天前
【软件测试】自动化测试
自动化测试·selenium·web自动化测试·ui自动化
ClouGence3 天前
告别低效脚本!8款更省事的 Selenium 替代工具推荐
selenium·测试