selenium---web頁面定位toast


  在寫app的時候介紹toast的定位,在web測試過程中,也經常遇到一些toast,那么這個toast我們這邊如何進行測試呢?

toast

toast屬於一種輕量級的反饋,常常以小彈框的形式出現,一般出現1到3秒會自動消失,可以出現在屏幕上中下任意位置,首先來看下web頁面上的toast是什么樣子的。

定位toast

這種toast,往往就存在3秒中左右,3秒中,打開瀏覽器這些內容都沒了,怎么定位呢?安靜給大家介紹一個小技巧。打開chrome進入F12頁面進入到Sources

通過正常定位來查看元素

這里安靜通過顯示等待的方法進行來定位toast。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get(r'E:\web\123.html')
driver.find_element_by_id('anjing').click()
# 元素位置
locator = (By.XPATH, '/html/body/div')
# 顯示等待獲取元素
WebDriverWait(driver, 20,0.5).until(EC.presence_of_element_located(locator))
# 獲取toast
t = driver.find_element_by_xpath('/html/body/div').text
print(t)

上面就是簡單的定位web頁面中的toast方法和一些小技巧。

下面提供安靜在網上找到toast的源碼,自己簡單的修改了下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>Toast</title>

</head>

<center> 
<body>
    <button id="anjing" onclick="clickme();">點擊關注</but-ton>  
</body>
</center>
<script>  

function showToast(msg,duration){  
    duration=isNaN(duration)?3000:duration;  
    var m = document.createElement('div');  
    m.innerHTML = msg;  
    m.style.cssText="width:60%; min-width:180px; background:#000; opacity:0.6; height:auto;min-height: 30px; color:#fff; line-height:30px; text-align:center; border-radius:4px; position:fixed; top:60%; left:20%; z-index:999999;";  
    document.body.appendChild(m);  
    setTimeout(function() {  
        var d = 0.5;  
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';  
        m.style.opacity = '0';  
        setTimeout(function() { document.body.removeChild(m) }, d * 1000);  
    }, duration);  
}  

function clickme(){
    showToast("感謝關注:測試-安靜",3000);
}

</script>   
</html>

 

 

如果感覺安靜寫的對您有幫助,點個關注,持續更新~~

 


免責聲明!

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



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