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

时间:2014-03-06 00:24:11

标签: selenium xpath download

我有一个包含多个下载链接的网页(所有pdf)。我正在使用selenium来测试它们(主要是为了下载所有这些pdf)。以下是代码中的spinet:

//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。引入等待也行不通。

2 个答案:

答案 0 :(得分:0)

出于兴趣,您可以尝试使用此css选择器;

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

答案 1 :(得分:0)

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

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