简体   繁体   中英

How to capture and extract this toast message using python's Selenium?

The toast pop out message just appears for two seconds. Its element is like

<div id class="layui-layer-content">abcde!</div>

How to capture the message and extract abcde!

Use Explicit Wait WebDriverWait() and wait for presence_of_element_located () and following css selector.

print(WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"div.layui-layer-content"))).text)

You need to import below libraries.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Try this:

div = driver.find_element_by_xpath('//div[@class = "layui-layer-content"]')
print(div.text)

Edit:

You can add this within a while loop to check whether the pop-up is present or not. If the pop-up is present you can display the text. Here is the code to do it:

while True:
    try:
        div = driver.find_element_by_xpath('//div[@class = "layui-layer-content"]')
        print(div.text)
        time.sleep(2)
    except:
        pass

Output:

卡号或密码错误!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM