无法对硒中具有相似 xpath 样式的元素执行单击操作



我有一个包含多个下载链接的网页(所有pdf)。我正在使用硒来测试它们(基本上是为了下载所有这些pdf)。下面给出的是代码中的尖刺:

//here's the list of all the elements   
        List<WebElement> elements = driver.findElements(By.xpath("//div[*]/ul/li/div/a[3]")); // total elements found: 278
            for (WebElement we:elements) {
            System.out.println(we.getText());
    //custom written wait method
        waitForElementPresent(By.xpath("//div[*]/ul/li/div/a[3]"),10); //executed 270 times
            we.click(); // only the last item (278th) is actually clicked.
            System.out.println("@click"); // this line is executed 278 times        
            }

它工作正常,但只下载最后一个pdf。引入等待也不起作用。

出于兴趣,您可以使用此 css 选择器尝试一下吗?

By.css("div>ul>li>div>a:nth-of-type(3)")

尝试更改下载设置 - 在单击下载链接之前插入以下代码片段:

FirefoxProfile profile = new FirefoxProfile(); 
        profile.setPreference("browser.download.folderList", 2);     
        profile.setPreference("browser.download.dir","C:\test");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,application/msword,application/x-rar-compressed,application/octet-stream,application/csv,text/csv,");      
        WebDriver driver = new FirefoxDriver(profile);
        driver.get("any site");

最新更新