首页 文章

在下面的HTML中,获取“元素当前不可见,因此可能无法与Selenium webdriver中的错误进行交互”

提问于
浏览
1

我收到“元素当前不可见,因此可能无法与selenium web-driver中的错误进行交互” .

在这里,我试图选择页面文本并单击按钮以进行其他操作但是给定的元素,在我在selenium web-driver中写的行下面 .

driver.findElement(By.cssSelector("div.annotator-adder")).click();

以下是按钮的HTML:

div class =“annotator-adder”style =“display:block; top:359px; left:585px;”> Annotate

以及用于文本选择的HTML,用于使用按钮添加注释:

“p section-number =”12-1-101.11.12“class =”缩写ICCSECONDPARA“>国家建筑师事务部已授权总务部负责审查和批准设计并遵守建设公立学校建筑和国有或国家租赁的基本服务建筑 . “

3 回答

  • 1

    尝试使用显式等待和Expected Conditions

    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.annotator-adder"))).click();
    

    这将等待最多20秒,以便在 click() 之前元素可见 .

  • 1

    看起来您的元素出现在页面上,但在浏览器视图端口中不可见 . 您可以先将该元素滚动到视图中,然后再进行操作 . 点击此处如何将元素滚动到视图中:Scroll Element into View with Selenium

  • 1

    由于等待不起作用,请尝试使用Thread.sleep(6000);

    您也可以尝试使用javascript executor来点击元素

    WebElement element = driver.findElement(By.cssSelector("div.annotator-adder"));
     JavascriptExecutor executor = (JavascriptExecutor)driver;
     executor.executeScript("arguments[0].click();", element);
    

    谢谢

相关问题