selenium3综合使用

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys  # 键盘操作使用的是Keys类,一般配合send_keys使用
from selenium.webdriver.support.wait import WebDriverWait  # 导入显性等待的包
from selenium.webdriver.support import expected_conditions as EC  # 判断所需要的元素是否已经被加载出来
from selenium.webdriver.common.by import By  # 定位
from selenium.webdriver import ActionChains  # 鼠标操作

# 通过百度搜素淘宝
driver = webdriver.Firefox()
driver.get('https://www.baidu.com/')
baidu_input = driver.find_element_by_id('kw')
baidu_input.send_keys('淘!我喜欢')
driver.get_screenshot_as_file("baidutaobao.png")
# 键盘操作 回车
baidu_input.send_keys(Keys.ENTER)
time.sleep(3)
# 从百度页面中进入淘宝
driver.find_element_by_partial_link_text('淘宝网 - ').click()
driver.get_screenshot_as_file("intotaobao.png")
# 跳转到淘宝登录页面
header = driver.window_handles
driver.switch_to.window(header[1])
driver.get_screenshot_as_file("taobao_home.png")
driver.find_element_by_css_selector('.site-nav-sign > a:nth-child(1)').click()

# 淘宝登录界面
driver.find_element_by_css_selector('.icon-qrcode').click()
driver.get_screenshot_as_file("taobao_home.png")


WebDriverWait(driver,15,0.5).until(EC.presence_of_element_located((By.ID,'q')))

tb_q = driver.find_element_by_id('q')
tb_q.clear()
tb_q.send_keys('华为mate40')
driver.find_element_by_css_selector('.btn-search').click()
driver.get_screenshot_as_file("phone_taobao.png")

driver.find_element_by_css_selector('#J_NavCommonRowItems_1 > a:nth-child(1) > span:nth-child(2)').click()
driver.get_screenshot_as_file("内存筛选.png")
driver.find_element_by_css_selector('#J_NavCommonRowItems_1 > a:nth-child(1) > span:nth-child(2)').click()
driver.get_screenshot_as_file("存储筛选.png")
driver.find_element_by_id('J_Itemlist_Pic_639558165249').click()

# start_money = driver.find_element_by_xpath(".//*[@id='J_relative']/div[1]/div/div[1]/div[1]/div/ul/li[1]/input")
# start_money.clear()
# start_money.send_keys(4000)
# end_money = driver.find_element_by_xpath(".//*[@id='J_relative']/div[1]/div/div[1]/div[1]/div/ul/li[3]/input")
# end_money.clear()
# end_money.send_keys(8000)

time.sleep(2)
# 加入购物车
header = driver.window_handles
driver.switch_to.window(header[2])
WebDriverWait(driver,15,0.5).until(EC.presence_of_element_located((By.CSS_SELECTOR,'dl.tb-prop:nth-child(2) > dd:nth-child(2) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)')))
# 网络类型
driver.find_element_by_css_selector('dl.tb-prop:nth-child(2) > dd:nth-child(2) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)').click()
time.sleep(1)
# 颜色
driver.find_element_by_css_selector('.tb-img > li:nth-child(1) > a:nth-child(1)').click()
time.sleep(1)
# 存储容量
driver.find_element_by_css_selector('dl.tb-prop:nth-child(5) > dd:nth-child(2) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)').click()
time.sleep(1)
# 花呗 li.progressive-item:nth-child(1) > a:nth-child(1)
driver.find_element_by_css_selector('li.progressive-item:nth-child(1) > a:nth-child(1)').click()
time.sleep(1)
driver.get_screenshot_as_file("保存之前的筛选.png")
driver.find_element_by_css_selector('#J_LinkBasket').click()
driver.get_screenshot_as_file("购物车保存.png")
time.sleep(2)
driver.close()

在这里插入图片描述

搜索淘宝

在这里插入图片描述

登录淘宝

在这里插入图片描述

搜索内存和存储筛选

在这里插入图片描述

选择手机参数

在这里插入图片描述

添加到购物车

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值