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


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

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

 

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

 

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

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

 

實戰題目

  1. 訪問:https://m.weibo.cn/
  2. 點擊:大家都在搜
  3. 點擊:微博熱搜榜
  4. 找到:實時熱點,每分鍾更新一次
  5. 將其中帶有 熱、沸、新字樣的熱搜信息獲取到,並注明屬於三種當中的哪一種

 

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

主要是第五步可能會有點困難

  1. 首先,定位到熱點列表
  2. 循環,先獲取熱點文本
  3. 然后,后面的圖標都是在放在 span 標簽里面的,所以要獲取span標簽
  4. 最后,獲取 img 標簽,通過圖片路徑 src 屬性判斷是屬於哪種熱點新聞

 

代碼

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

"""
__title__  = 
__Time__   = 2020/3/25 14:08
__Author__ = 小菠蘿測試筆記
__Blog__   = https://www.cnblogs.com/poloyy/
"""
from time import sleep

from selenium import webdriver

# 需要將驅動路徑改成自己的路徑哦
driver = webdriver.Chrome(executable_path=r"../resources/chromedriver.exe")

url = "https://m.weibo.cn/"

driver.get(url)

# 點擊搜索框
driver.find_element_by_class_name("m-search").click()

sleep(2)

# 點擊【微博實時搜索】
driver.find_element_by_class_name("card-main").find_elements_by_class_name("m-item-box")[-1].click()

sleep(2)

# 查找list
lists = driver.find_element_by_class_name("card11").find_element_by_class_name("card-list").find_elements_by_class_name("card4")

# 循環熱搜列表
for i in lists:
    text = i.find_element_by_class_name("main-text").text
    span = i.find_elements_by_class_name("m-link-icon")
    if span:
        src = span[0].find_element_by_tag_name("img").get_attribute("src")

        if "hot" in src:
            print(f"{text} 是 很熱的頭條")
        elif "new" in src:
            print(f"{text} 是 新的頭條")
        elif "fei" in src:
            print(f"{text} 是 沸騰的頭條")
        elif "recom" in src:
            print(f"{text} 是 推薦的頭條")
        else:
            print(f"{text} 是 普通的頭條")

 


免責聲明!

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



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