Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 遍历网页页脚部分中的链接_Java_Selenium_Selenium Webdriver_Automated Tests_Selenium Chromedriver - Fatal编程技术网

Java 遍历网页页脚部分中的链接

Java 遍历网页页脚部分中的链接,java,selenium,selenium-webdriver,automated-tests,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Automated Tests,Selenium Chromedriver,我试图在页脚的某个部分中遍历链接。我已经写了这段代码,但一直失败。我希望有人能告诉我我做错了什么,我该如何解决这个问题 import java.awt.List; import java.util.Iterator; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebE

我试图在页脚的某个部分中遍历链接。我已经写了这段代码,但一直失败。我希望有人能告诉我我做错了什么,我该如何解决这个问题

import java.awt.List;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class HomeWork {

public static void main(String[] args) throws InterruptedException {
    System.setProperty("webdriver.chrome.driver", "C:\\Work\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    String url = "http://automationpractice.com/index.php?controller=authentication&back=my-account";
    driver.get(url);



    WebElement infoLinkSec = driver.findElement(By.xpath("//*[@id=\"block_various_links_footer\"]"));
    int noOfLink = infoLinkSec.findElements(By.tagName("a")).size();
    System.out.println(noOfLink);


    Iterator<WebElement> links = infoLinkSec.findElements(By.tagName("a")).iterator();





    WebElement[] link = new WebElement[noOfLink];


    for (int i = 0; i< noOfLink; i++ ) {
        link[i] = links.next();
            }


    for (int i = 0; i< noOfLink; i++ ) {

        link[i].click();
        Thread.sleep(2000L);

        String cUrl = driver.getCurrentUrl();
        String cPageTitle = driver.getTitle();
        System.out.println((i+1) +". The current url is "+cUrl+"\nThe current page title is "+cPageTitle);

        driver.navigate().back();
        driver.navigate().refresh();
        Thread.sleep(3000L);
        }


}

    }

因此,这里似乎出现了一个
StaleElementReferenceException
。您说过“问题似乎是当我们返回并刷新时”,这是完全正确的——这就是您遇到此异常的原因

当您找到
WebElement
WebElement
列表并将其存储在变量中,然后更改页面(通过刷新或转到其他页面),您对该元素的引用将不再有效。您的变量指向已过时的元素

刷新页面后,可以通过重新查找与交互的所有元素来解决此问题

因此,在
driver.navigate.refresh()之后
link[i]。click()
将引发异常,因为
link[]
现在包含页面上不再有效的元素列表。您需要重新调用
driver.findElements
link[]
设置回新元素的状态

设置
link[]
变量的代码在这里似乎有点复杂,所以刷新后只调用
driver.findElements
并不是那么容易。您可能需要重构代码,以便在每次刷新后轻松找到一组新的链接:

System.setProperty("webdriver.chrome.driver", "C:\\Work\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

String url = "http://automationpractice.com/index.php?controller=authentication&amp;back=my-account";
driver.get(url);

// get the links
List<WebElement> links = driver.findElement(By.xpath("//*[@id='block_various_links_footer']//a"));

// iterate links in a for loop
for (int i = 0; i < links.size(); i++)
{
    // click the link
    link[i].click();
    Thread.sleep(2000L);

    // print page info
    String cUrl = driver.getCurrentUrl();
    String cPageTitle = driver.getTitle();
    System.out.println((i+1) +". The current url is "+cUrl+"\nThe current page title is "+cPageTitle);

    // go back
    driver.navigate().back();
    driver.navigate().refresh();
    Thread.sleep(3000L);

    // re-find links list
    List<WebElement> links = driver.findElement(By.xpath("//*[@id='block_various_links_footer']//a"));
}
System.setProperty(“webdriver.chrome.driver”、“C:\\Work\\chromedriver\u win32\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
字符串url=”http://automationpractice.com/index.php?controller=authentication&;返回=我的帐户”;
获取(url);
//获取链接
List links=driver.findElement(By.xpath(“//*[@id='block\u各种链接\u footer']///a”);
//在for循环中迭代链接
对于(int i=0;i。当前url为“+cUrl+”\n当前页面标题为“+cPageTitle”);
//回去
driver.navigate().back();
driver.navigate().refresh();
线程。睡眠(3000L);
//重新查找链接列表
List links=driver.findElement(By.xpath(“//*[@id='block\u各种链接\u footer']///a”);
}

Selenium提供了一个有用的链接,指向堆栈跟踪中的on
StaleElementReferenceException
。如果你特别好奇,可以在他们的页面上阅读更多内容。

嗨,克里斯汀;非常感谢你的帮助,我非常感谢你花时间和精力帮助我。克里斯汀,我已经按照你的建议重写了代码,但是我还是得到了同样的错误。您是否认为迭代器()可能不是单击链接的最佳方式,如果您这样做,您的方法会是什么样子。谢谢您,我非常感谢您的帮助。@YSamKabir我已经用您的代码的简化版本更新了答案。这是在C#中测试的,但转换为Java时,功能应该是相同的。
System.setProperty("webdriver.chrome.driver", "C:\\Work\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

String url = "http://automationpractice.com/index.php?controller=authentication&amp;back=my-account";
driver.get(url);

// get the links
List<WebElement> links = driver.findElement(By.xpath("//*[@id='block_various_links_footer']//a"));

// iterate links in a for loop
for (int i = 0; i < links.size(); i++)
{
    // click the link
    link[i].click();
    Thread.sleep(2000L);

    // print page info
    String cUrl = driver.getCurrentUrl();
    String cPageTitle = driver.getTitle();
    System.out.println((i+1) +". The current url is "+cUrl+"\nThe current page title is "+cPageTitle);

    // go back
    driver.navigate().back();
    driver.navigate().refresh();
    Thread.sleep(3000L);

    // re-find links list
    List<WebElement> links = driver.findElement(By.xpath("//*[@id='block_various_links_footer']//a"));
}