首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在使用sendKeys打开多个选项卡时,Java“元素不可交互的异常”

在使用sendKeys打开多个选项卡时,Java“元素不可交互的异常”
EN

Stack Overflow用户
提问于 2022-01-18 02:44:15
回答 1查看 163关注 0票数 1

我试图在网上刮一个魁北克政府网站的法律名称和相关的PDF,但当我试图打开所有不同的法律选项卡,以获得他们的PDF链接,我得到一个ElementNotInteractable的例外,当它试图打开第9链接。我试着自己打开链接,它打开得很好,但是当它通过所有的法律时,它就停止了,给了我这个例外。下面是我的代码片段:

代码语言:javascript
运行
复制
static SortedMap<String,String> QuebecConsolidatedStatutesAndPDFs = new TreeMap<String,String>();
   
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\WorkSpace\\Driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5000));
        driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=statutes&selection=all");
        Thread.sleep(5000);

        List<WebElement> QuebecConsolidatedStatutes = driver.findElements(By.xpath("//body/div/div/div[2]/div/div[2]/table/tbody/tr[contains(@class, 'clickable')]/td/a"));
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        String parent = driver.getWindowHandle();
        for (int i=0; i<QuebecConsolidatedStatutes.size(); i++){
            String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
            
            wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
            QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
        
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-18 07:30:14

这里有几个问题:

  1. 主要问题是必须滚动要单击的元素到视图中。默认的初始屏幕高度显示8行,而要单击第9行或更多行,则必须先将该元素滚动到视图中。implicitlyWait.

  • 您可以将驱动程序窗口设置为更好的尺寸,这将显示更多的屏幕,但是仍然需要滚动,但在15个元素之后。

  • 应该改进您的定位器。

  • 您不应该混淆WebDriverWait

这应该更好地发挥作用:

代码语言:javascript
运行
复制
static SortedMap<String,String> QuebecConsolidatedStatutesAndPDFs = new TreeMap<String,String>();
   
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\WorkSpace\\Driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5000));
        driver.manage().window().maximize();
        driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=statutes&selection=all");
        wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("tr.clickable a"), 100));
        Thread.sleep(300);

        List<WebElement> QuebecConsolidatedStatutes = driver.findElements(By.cssSelector("tr.clickable a"));
        String parent = driver.getWindowHandle();
        for (int i=0; i<QuebecConsolidatedStatutes.size(); i++){
            String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
            
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", QuebecConsolidatedStatutes.get(i));
            Thread.sleep(300);
            wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
            QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70749759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档