现在我使用的代码如下:
msg = driver.find_element_by_css_selector ('div#msg').get_attribute ('style')
if 'display: none;' in msg:
print("window doesn't popup")
if 'display:block;' in msg:
print('windows popup')
但是,如果我不在消息上方添加time.sleep(0.1)
:
time.sleep(0.1)
msg = driver.find_element_by_css_selector ('div#msg').get_attribute ('style')
有时结果并不准确。但我需要非常快速的测试,所以我想知道有没有更好的方法,我可以检查是否有弹出窗口,或者有什么方法可以减少等待时间?
我刚刚将您的选择器转换为xpath并与逻辑@style!=""
组合,然后使用Explicit Waits
实现它:
msg = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//div[@id="msg" and @style!=""]'))).get_attribute('style')
if 'display: none;' in msg:
print("window doesn't popup")
if 'display:block;' in msg:
print('windows popup')
以下导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC