簡體   English   中英

使用selenium webdriver在同一頁面上逐個單擊鏈接

[英]Clicking on link one by one from the same page using selenium webdriver

我正在嘗試點擊狀態鏈接給定網址,然后打印下一頁的標題然后返回,然后使用for循環動態單擊另一個狀態鏈接。 但循環在初始值后停止。
我的代碼如下:

 public class Allstatetitleclick {  
    public static void main(String[] args) {        
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.adspapa.com/");      

        WebElement catBox = driver.findElement(By.xpath("html/body/table[1]/tbody/tr/td/table/tbody/tr/td[1]/table"));
        List<WebElement> catValues = catBox.findElements(By.tagName("a"));

        System.out.println(catValues.size());

        for(int i=0;i<catValues.size();i++){

            //System.out.println(catValues.get(i).getText());       
            catValues.get(i).click();
            System.out.println(driver.getTitle());
            driver.navigate().back();

        }
        System.out.println("TEST");
    }
}

問題是在導航回來之前找到的元素將會過期。 因此,我們需要更新代碼以在導航后重新引導元素。

更新以下代碼:

        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.adspapa.com/");      

        WebElement catBox = driver.findElement(By.xpath("html/body/table[1]/tbody/tr/td/table/tbody/tr/td[1]/table"));
        List<WebElement> catValues = catBox.findElements(By.tagName("a"));

        for(int i=0;i<catValues.size();i++)
        { 
            catValues.get(i).click();
            System.out.println(driver.getTitle());
            driver.navigate().back();
            catBox = driver.findElement(By.xpath("html/body/table[1]/tbody/tr/td/table/tbody/tr/td[1]/table"));
            catValues = catBox.findElements(By.tagName("a"));
        }
        driver.quit();

我已在上面測試了上面的代碼並且工作正常。您可以根據您的使用改進上面的代碼。

注意:我已經執行了這個並且它現在工作正常。它會給你大小並點擊每個鏈接。這是你需要進入框架的問題。 它有一個框架,您需要切換。 請復制並粘貼並執行它。 希望你找到答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM