Selenium系列(十九) - Web UI 自動化基礎實戰(6)


如果你還想從頭學起Selenium,可以看看這個系列的文章哦!

https://www.cnblogs.com/poloyy/category/1680176.html

 

其次,如果你不懂前端基礎知識,需要自己去補充哦,博主暫時沒有總結(雖然我也會,所以我學selenium就不用復習前端了哈哈哈...)

 

注意,目前的實戰都是流水賬式寫的,后面才會結合框架+PO模式

目的是為了掌握所學的Selenium基礎

 

實戰題目

  1. 訪問: https://www.vmall.com/
  2. 獲取一級菜單下包含哪些二級菜單,不包含查看全部
  3. 然后獲取下面,熱銷單品中所有 頂部 帶有 爆款字樣的產品名稱及價格

 

代碼思路(人為測試時的操作步驟)

  1. 定位一級菜單的選項列表
  2. 循環列表,每次都將鼠標懸浮在當前選項上,然后打印二級菜單的列表
  3. 熱銷單品在頁面下方,需要滑動頁面
  4. 定位熱銷單品列表
  5. 循環,獲取標題和價格,打印爆款

 

代碼

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  = 
__Time__   = 2020/4/2 20:04
__Author__ = 小菠蘿測試筆記
__Blog__   = https://www.cnblogs.com/poloyy/
"""
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains

driver = 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 element


def 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}")
    # hover
    action.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}")

# 往下滾動1000px
js = "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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM