elem = driver.find_element_by_id("msgButtonAffirm")
if elem.is_displayed():
elem.click()
print("conform popup is avalable and click")
else:
print "Pop-up is not visible"
elem = driver.find_elements_by_id("msgButtonAffirm")
if elem and elem[0].is_displayed():
elem[0].click()
print("conform popup is avalable and click")
else:
print("Pop-up is not visible")
from selenium.common.exceptions import NoSuchElementException
try:
elem = driver.find_element_by_id("msgButtonAffirm")
elem.click()
print("conform popup is avalable and click")
except NoSuchElementException:
print("Pop-up is not visible")
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#msgButtonAffirm"))).click()
print ("Pop-up was clickable and clicked")
except TimeoutException:
print ("Pop-up was not clickable")
使用XPATH:
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='msgButtonAffirm']"))).click()
print ("Pop-up was clickable and clicked")
except TimeoutException:
print ("Pop-up was not clickable")
注意:必须添加以下导入:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您可以使用
find_elements_by_id
并检查列表中是否有任何内容您可以导入异常并按以下方式进行处理:
你需要注意几件事:
NoSuchElementException
相关的HTML可以帮助我们更好地分析这个问题。但是,根据上述要点,您需要将WebDriverWaitfor expected_conditions归纳为^{} ,并且您可以使用以下Locator Strategies之一:
使用
CSS_SELECTOR
:使用
XPATH
:注意:必须添加以下导入:
相关问题 更多 >
编程相关推荐