[英]Check if element is clickable in Selenium Java
我是Selenium
的新手,需要检查元素在Selenium
Java
中是否可点击,因为element.click()
在link
和label
上都通过。
我尝试使用以下代码,但它不起作用:
WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);
if(wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")))==null)
elementToBeClickable
用于检查元素是否可见并启用,以便您可以单击它。
如果预期条件为真, ExpectedConditions.elementToBeClickable
返回WebElement
否则将抛出TimeoutException
,它永远不会返回null
。
因此,如果您使用ExpectedConditions.elementToBeClickable
来查找始终为您提供可点击元素的元素,因此无需检查null
条件,您应该尝试如下:-
WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")));
element.click();
正如您所说element.click()
在link
和label
上都传递这并不意味着元素不可点击,这意味着clicked
了返回的元素,但可能没有通过点击操作对元素执行事件。
注意:- 我建议你总是先尝试通过id
、 name
、 className
和其他定位器来查找元素。 如果您遇到一些困难,请使用cssSelector
并始终将xpath
定位器放在最后,因为它比其他定位器定位元素要慢。
希望它可以帮助你.. :)
在某些情况下element.isDisplayed() && element.isEnabled()
将返回true
但仍然无法单击元素,因为它被其他元素隐藏/重叠。
在这种情况下,捕获的Exception
是:
org.openqa.selenium.WebDriverException:未知错误:元素在点(781、704)不可点击。 其他元素会收到点击:
<div class="footer">...</div>
请改用此代码:
WebElement element=driver.findElement(By.xpath"");
JavascriptExecutor ex=(JavascriptExecutor)driver;
ex.executeScript("arguments[0].click()", element);
它会起作用的。
wait.until(ExpectedConditions)
不会返回 null,它要么满足条件,要么抛出TimeoutException
。
您可以检查元素是否显示并启用
WebElement element = driver.findElement(By.xpath);
if (element.isDisplayed() && element.isEnabled()) {
element.click();
}
有一些事情你必须注意:
until
条件下默认遇到的NotFoundException
实例。 要在元素返回后立即调用click()
,可以使用:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))).click();
要简单地验证元素是否已定位且可点击,请将WebDriverWait包装在try-catch{}
块中,如下所示:
try { new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))); System.out.println("Element is clickable"); } catch(TimeoutException e) { System.out.println("Element isn't clickable"); }
如果WebDriverWait返回定位到的可点击元素,但该元素仍然不可点击,则需要调用executeScript()
方法,如下所示:
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))); ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
从源代码中可以看到, ExpectedConditions.elementToBeClickable()
会判断元素是否可见并已启用,因此可以将isEnabled()
与isDisplayed()
) 一起使用。 以下是源代码。
public static ExpectedCondition<WebElement> elementToBeClickable(final WebElement element) { return new ExpectedCondition() { public WebElement apply(WebDriver driver) { WebElement visibleElement = (WebElement) ExpectedConditions.visibilityOf(element).apply(driver); try { return visibleElement != null && visibleElement.isEnabled() ? visibleElement : null; } catch (StaleElementReferenceException arg3) { return null; } } public String toString() { return "element to be clickable: " + element; } }; }
当元素不可点击时,类属性包含disabled
。
WebElement webElement = driver.findElement(By.id("elementId"));
if(!webElement.getAttribute("class").contains("disabled")){
webElement.click();
}
List<WebElement> wb=driver.findElements(By.xpath(newXpath));
for(WebElement we: wb){
if(we.isDisplayed() && we.isEnabled())
{
we.click();
break;
}
}
}
有一个名为“cursor”的 CSS 属性,用于确定当鼠标指针悬停在元素上时要显示的鼠标光标类型。
它用于使元素可点击,因此可用于检查元素是否可点击。
显示和启用在我的情况下不起作用,当元素可见但不可点击时,我确实如此。
if (AddDocumentButton.Displayed & AddDocumentButton.GetCssValue("cursor").Equals("pointer") & AddDocumentButton.GetCssValue("color").Equals("rgb(22, 172, 0)"))
{
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.