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)

相关推荐
一个天蝎座 白勺 程序猿4 小时前
Python爬虫(29)Python爬虫高阶:动态页面处理与云原生部署全链路实践(Selenium、Scrapy、K8s)
redis·爬虫·python·selenium·scrapy·云原生·k8s
代码的乐趣7 小时前
支持selenium的chrome driver更新到136.0.7103.94
chrome·python·selenium
深浅卡布星20 小时前
Postman启动时检测到版本不匹配错误
测试工具·postman
九章云极AladdinEdu1 天前
GPU与NPU异构计算任务划分算法研究:基于强化学习的Transformer负载均衡实践
java·开发语言·人工智能·深度学习·测试工具·负载均衡·transformer
程序员小远1 天前
UI自动化测试方案详解
自动化测试·软件测试·selenium·测试工具·ui·职场和发展·测试用例
JZMSYYQ1 天前
磁光克尔效应在量子计算中的应用
功能测试·测试工具·制造
逸雨清风2 天前
Chrome更新到136以后selenium等自动化浏览器失效
selenium·自动化
小白学大数据2 天前
Python+Selenium爬虫:豆瓣登录反反爬策略解析
分布式·爬虫·python·selenium
小冯的编程学习之路3 天前
【软件测试】:推荐一些接口与自动化测试学习练习网站(API测试与自动化学习全攻略)
c++·selenium·测试工具·jmeter·自动化·测试用例·postman
Ten peaches3 天前
Selenium-Java版(操作元素)
java·selenium·测试工具·html