代码之家  ›  专栏  ›  技术社区  ›  Erik van den Hoorn

如何使用Selenium检查元素是否可单击-使用Javascript[duplicate]的WebDriver

  •  0
  • Erik van den Hoorn  · 技术社区  · 7 年前

    我使用了显式等待,我得到了警告:

    命令持续时间或超时:393毫秒

    如果我使用 Thread.sleep(2000)

    @Test(dataProvider = "menuData")
    public void Main(String btnMenu, String TitleResultPage, String Text) throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        driver.findElement(By.id("navigationPageButton")).click();
    
        try {
           wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu)));
        } catch (Exception e) {
            System.out.println("Oh");
        }
        driver.findElement(By.cssSelector(btnMenu)).click();
        Assert.assertEquals(driver.findElement(By.cssSelector(TitleResultPage)).getText(), Text);
    }
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   sweta kumari    5 年前

    org.openqa.selenium.WebDriverException 延伸到 .


    关于您个人的用例,错误告诉了我们:

    WebDriverException: Element is not clickable at point (x, y). Other element would receive the click 
    

    从代码块中可以清楚地看出,您已经定义了 wait 作为 WebDriverWait wait = new WebDriverWait(driver, 10); 但你是在打电话给 click() ExplicitWait 开始发挥作用,如 until(ExpectedConditions.elementToBeClickable)

    解决方案

    Element is not clickable at point (x, y) 可能由不同的因素引起。您可以通过以下任一过程来解决这些问题:

    尝试使用 Actions 班级:

    WebElement element = driver.findElement(By.id("navigationPageButton"));
    Actions actions = new Actions(driver);
    actions.moveToElement(element).click().build().perform();
    

    Viewport

    尝试使用 JavascriptExecutor

    WebElement myelement = driver.findElement(By.id("navigationPageButton"));
    JavascriptExecutor jse2 = (JavascriptExecutor)driver;
    jse2.executeScript("arguments[0].scrollIntoView()", myelement); 
    

    3.在元素可单击之前,页面正在刷新。

    在这种情况下 WebDriverWait网站 如第4点所述。

    4.元素存在于DOM中,但不可单击。

    明确 具有 ExpectedConditions 设置为 elementToBeClickable 对于要单击的元素:

    WebDriverWait wait2 = new WebDriverWait(driver, 10);
    wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));
    

    明确 具有 预期条件 设置为 invisibilityOfElementLocated 使覆盖层不可见。

    WebDriverWait wait3 = new WebDriverWait(driver, 10);
    wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
    

    6.元素存在,但具有永久覆盖层。

    使用 JavascriptExecutor

    WebElement ele = driver.findElement(By.xpath("element_xpath"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", ele);