检查元素在硒中是否可点击 [英] Check whether element is clickable in selenium
问题描述
我可以验证元素是否存在以及是否显示,但似乎无法找到一种方法来查看它是否是可点击的(不是说禁用)。
问题在于,当填充webform时,我想要的元素可能会在加载时用div覆盖。 div本身很难被发现,因为它是id,名字,甚至css都很灵活。因此,我试图检测是否可以点击或填充输入字段。当覆盖div存在时,该字段不能由普通用户填充(因为div将覆盖输入字段并且不允许用户填充它),但它可以由硒填充。我想阻止这种情况,只有在用户也可以填充硒时才允许硒填充它。
=http://selenium-python.readthedocs.org/api.html#selenium.webdriver.support.expected_conditions.element_to_be_clickable =noreferrer> 等待元素点击 a>: from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui从selenium.webdriver.support导入WebDriverWait
导入expected_conditions作为EC
驱动程序= webdriver.Firefox()
driver.get(http:/ / somedomain)
element = WebDriverWait(driver,10).until(
EC.element_to_be_clickable((By.ID,myDynamicElement))
)
或者,您可以按照 EAFP
原则并捕获异常为r alen by click()
:
from selenium.common.exceptions import WebDriverException
尝试:
element.click()
除WebDriverException外:
print元素不可点击
I'm able to verify whether or not an element exists and whether or not it is displayed, but can't seem to find a way to see whether it is 'clickable' (Not talking about disabled).
Problem is that often when filling a webform, the element i want may be overlayed with a div while it loads. The div itself is rather hard to detect as it's id, name and even the css is flexible. Hence i'm trying to detect whether or not a input field can be 'clicked' or 'filled'. When the overlaying div is present, the field cannot be filled by a regular user (As the div would overlay the input field and not allow the user to fill it) but it can be filled by selenium. I want to prevent that and only allow selenium to fill it once the user can also fill it.
You can wait for the element to be clickable:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain")
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "myDynamicElement"))
)
Or, you can follow the EAFP
principle and catch an exception being raised by click()
:
from selenium.common.exceptions import WebDriverException
try:
element.click()
except WebDriverException:
print "Element is not clickable"
这篇关于检查元素在硒中是否可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!