注意,目前的实战都是流水账式写的,后面才会结合框架+PO模式
目的是为了掌握所学的Selenium基础
实战题目
- 访问: https://www.vmall.com/
- 获取一级菜单下包含哪些二级菜单,不包含查看全部
- 然后获取下面,热销单品中所有 顶部 带有 爆款字样的产品名称及价格
代码思路(人为测试时的操作步骤)
- 定位一级菜单的选项列表
- 循环列表,每次都将鼠标悬浮在当前选项上,然后打印二级菜单的列表
- 热销单品在页面下方,需要滑动页面
- 定位热销单品列表
- 循环,获取标题和价格,打印爆款
代码
#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__ =__Time__ = 2020/4/2 20:04__Author__ = 小菠萝测试笔记__Blog__ = https://www.cnblogs.com/poloyy/"""from time import sleepfrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ecfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.common.action_chains import ActionChainsdriver = webdriver.Chrome("../resources/chromedriver.exe")action = ActionChains(driver)def wait_element(by_, element_):element = WebDriverWait(driver, timeout=10).until(ec.presence_of_element_located((by_, element_)))return elementdef wait_elements(by_, element_):element = WebDriverWait(driver, timeout=10).until(ec.presence_of_all_elements_located((by_, element_)))return element# 打开网站driver.get("https://www.vmall.com/")# 列表lists = wait_elements(By.XPATH, '//div[@id="category-block"]/div/ol/li')for one in lists:one_v = one.find_element_by_xpath("./input[2]").get_attribute("value")print(f"一级菜单:{one_v}")# hoveraction.move_to_element(one).perform()# hover出来的面板items = one.find_elements_by_xpath('./div[contains(@class,"category-panels")]/ul/li[@class="subcate-item"]')for item in items:value = item.find_element_by_xpath('./input[1]').get_attribute("value")print(f"\t{value}")# 往下滚动1000pxjs = "document.documentElement.scrollTop = 1000"driver.execute_script(js)# 打印爆款hot_lists = driver.find_elements_by_xpath('//div[contains(@class,"home-hot-goods")]//ul[@class="grid-list clearfix"]/li')for hot in hot_lists:title = hot.find_element_by_xpath('./a/div[@class="grid-title"]')price = hot.find_element_by_xpath('./a/p[@class="grid-price"]')print(f"爆款:{title.text}, 价格:{price.text}")sleep(5)driver.quit()


