检查Selenium Java中的元素是否可单击

iih3973s  于 10个月前  发布在  Java
关注(0)|答案(7)|浏览(134)

我是Selenium的新手,需要检查SeleniumJava中的元素是否可单击,因为element.click()同时传递linklabel
我尝试使用以下代码,但它不工作:

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)

字符串

5t7ly7z5

5t7ly7z51#

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()同时传递linklabel,这并不意味着元素是不可点击的,它意味着返回的元素clicked,但可能是没有事件执行的元素点击操作。

注意:-我建议你总是先尝试通过idnameclassName和其他定位器查找元素。如果你遇到了一些困难,那么使用cssSelector,并始终给予xpath定位器最后优先级,因为它比其他定位器定位元素要慢。

希望对你有帮助:)

3ks5zfa0

3ks5zfa02#

在某些情况下,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);

字符串
会成功的

6uxekuva

6uxekuva3#

wait.until(ExpectedConditions)不会返回null,它要么满足条件,要么抛出TimeoutException
您可以检查元素是否显示并启用

WebElement element = driver.findElement(By.xpath);
if (element.isDisplayed() && element.isEnabled()) {
    element.click();
}

字符串

wz1wpwve

wz1wpwve4#

有一些事情你必须要注意:

  • WebDriverWaitExpectedConditions一起作为elementToBeClickable()在WebElement被 * 定位 * 且 * 可点击 * 时返回WebElement,即可见启用
  • 在此过程中,WebDriverWait将忽略默认情况下在until条件中遇到的NotFoundException的示例。
  • 一旦 wait 的持续时间因所需元素未被 * 定位 * 和 * 可单击 * 而过期,将引发超时异常。
  • 解决这一问题的不同方法是:
  • 要在返回元素后立即调用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 传回 locatedclickable 元素,但该元素仍无法按一下,您需要叫用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);

csbfibhn

csbfibhn5#

class属性在元素不可点击时包含disabled

WebElement webElement = driver.findElement(By.id("elementId"));
if(!webElement.getAttribute("class").contains("disabled")){
    webElement.click();
}

字符串

e0uiprwp

e0uiprwp6#

从源代码中可以看到,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;
            }
        };
    }

字符串

sg3maiej

sg3maiej7#

List<WebElement> wb=driver.findElements(By.xpath(newXpath));
        for(WebElement we: wb){
            if(we.isDisplayed() && we.isEnabled())
            {
                we.click();
                break;
            }
        }
    }

字符串

相关问题