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()
selenium3综合使用
最新推荐文章于 2024-05-06 16:56:47 发布