Home > Back-end >  Java Selenium "Element Not Interactable Exception" when using sendKeys to open multiple ta
Java Selenium "Element Not Interactable Exception" when using sendKeys to open multiple ta

Time:01-19

I am trying to web scrape a Quebec government website for law names and their associated PDFs but when I try to open the tabs of all the different laws to get their PDF links, I get an ElementNotInteractable Exception when it attempts to open the 9th link. I tried opening the link by itself and it opens fine but when it is going through all the laws, it stops there and gives me that exception. Here is my code snippet:

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);
        
        }

CodePudding user response:

There are several issues here:

  1. The main problem is that you have to scroll the element you want to click on into the view. Your default initial screen height presents 8 rows while to click on 9-th row and more you have to scroll that element first into the view.
  2. You could set driver window to better dimensions, this will show you more screen, however you will still have to scroll, but after 15 elements.
  3. You should improve your locators.
  4. You should not mix up WebDriverWait and implicitlyWait.

This should work better:

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(50);

        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));
            wait(300);
            wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
            QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
    }
}
  •  Tags:  
  • Related