Python 检查元素在硒中是否可点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26943847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check whether element is clickable in selenium
提问by Aayush Agrawal
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.
问题是,通常在填写 webform 时,我想要的元素在加载时可能会被 div 覆盖。div 本身很难检测,因为它的 id、name 甚至 css 都是灵活的。因此,我试图检测输入字段是否可以“点击”或“填充”。当覆盖 div 存在时,该字段不能由普通用户填充(因为 div 会覆盖输入字段并且不允许用户填充它)但它可以由 selenium 填充。我想防止这种情况发生,并且只有在用户也可以填充时才允许硒填充它。
回答by alecxe
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 EAFPprincipleand catch an exception being raised by click():
或者,您可以遵循以下EAFP原则并捕获由click()以下方式引发的异常:
from selenium.common.exceptions import WebDriverException
try:
element.click()
except WebDriverException:
print "Element is not clickable"
回答by MANOR
You can use webElement.isEnabled()and webElement.isDisplayed()methods before performing any operation on the input field...
您可以在对输入字段执行任何操作之前使用webElement.isEnabled()和webElement.isDisplayed()方法...
I hope this will solve you problem...
我希望这能解决你的问题......
Else, you can also put a loop to check above 2 conditions and if those are true you can specify the input text and again you can find the same element and you can get the text of the webelement and compare that text with the text you entered. If those matches you can come out of the loop.
否则,您还可以放置一个循环来检查以上 2 个条件,如果这些条件为真,您可以指定输入文本,然后您可以再次找到相同的元素,您可以获得 webelement 的文本并将该文本与您输入的文本进行比较. 如果这些匹配,您可以退出循环。
回答by wutzebaer
to click an (for selenium) unreachable element i alwas use:
单击我一直使用的(对于硒)无法访问的元素:
public void clickElement(WebElement el) throws InterruptedException {
((JavascriptExecutor) driver).executeScript("arguments[0].click();", el);
}

