Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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 导航回selenium WebDriver时出现StaleElementReferenceException_Java_Javascript_Selenium_Selenium Webdriver - Fatal编程技术网

Java 导航回selenium WebDriver时出现StaleElementReferenceException

Java 导航回selenium WebDriver时出现StaleElementReferenceException,java,javascript,selenium,selenium-webdriver,Java,Javascript,Selenium,Selenium Webdriver,我正在尝试使用SeleniumWebDriver在网页中的一些锚固件中爬行。我能想到的方法是在列表中获得锚,并在每次单击后执行单击和向后导航。这是我的密码: WebDriver webDriver=new FirefoxDriver(); webDriver.get(SEARCH_URL); WebElement form2=webDriver.findElement(By.id("frmMain")); form2.submit(

我正在尝试使用SeleniumWebDriver在网页中的一些锚固件中爬行。我能想到的方法是在列表中获得锚,并在每次单击后执行单击和向后导航。这是我的密码:

        WebDriver webDriver=new FirefoxDriver();
        webDriver.get(SEARCH_URL);
        WebElement form2=webDriver.findElement(By.id("frmMain"));
        form2.submit();
        System.out.println(webDriver.getCurrentUrl());
        List<WebElement>doctorAnchors=webDriver.findElements(By.xpath("//td[@class='data']/a"));
        int count=0;
        for(WebElement anchr:doctorAnchors){
            anchr.click();
            System.out.println((count++)+" : "+webDriver.getPageSource().toString());
            Thread.sleep(10000);
            webDriver.navigate().back();
        }

我浏览了关于同一个异常的各种stackoverflow帖子,发现这可能是由于页面中的一些javascript内容造成的,这似乎也是正确的,因为页面url是:
http://www.somepage.com/dispatch
我是否去任何一个锚点。我可以像往常一样在驱动程序打开的web浏览器中导航回去。但是为什么这
webDriver.navigate().back()
失败了?点击链接后,我该如何返回?是否有一种方法可以保存驱动程序的状态,然后单击并在单击后恢复该状态?

我下面给出的是您的解决方案示例。在这里,我调用了一个方法,
getElementWithIndex
。。这个有用

在本例中,它捕获特定帧|类| id下的所有链接并逐个导航

driver.get("www.xyz.com");  
WebElement element = driver.findElement(By.id(Value));
List<WebElement> elements = element.findElements(By.tagName("a"));
int sizeOfAllLinks = elements.size();
System.out.println(sizeOfAllLinks);

for(int i=0; i<sizeOfAllLinks ;i++)
{
     System.out.println(elements.get(i).getAttribute("href"));
}

for (int index=0; index<sizeOfAllLinks; index++ )
{
     getElementWithIndex(By.tagName("a"), index).click();
     driver.navigate().back();
}

public WebElement getElementWithIndex(By by, int index)
{
     WebElement element = driver.findElement(By.id(Value));
     List<WebElement> elements = element.findElements(By.tagName("a")); 
     return elements.get(index);
}
driver.get(“www.xyz.com”);
WebElement=driver.findElement(By.id(Value));
列表元素=元素.findElements(按.tagName(“a”));
int-sizeOfAllLinks=elements.size();
System.out.println(sizeOfAllLinks);

对于(int i=0;i我下面给出的是一个解决方案的示例。在这里,我调用了一个方法,
getElementWithIndex
。这样做很有效

在本例中,它捕获特定帧|类| id下的所有链接并逐个导航

driver.get("www.xyz.com");  
WebElement element = driver.findElement(By.id(Value));
List<WebElement> elements = element.findElements(By.tagName("a"));
int sizeOfAllLinks = elements.size();
System.out.println(sizeOfAllLinks);

for(int i=0; i<sizeOfAllLinks ;i++)
{
     System.out.println(elements.get(i).getAttribute("href"));
}

for (int index=0; index<sizeOfAllLinks; index++ )
{
     getElementWithIndex(By.tagName("a"), index).click();
     driver.navigate().back();
}

public WebElement getElementWithIndex(By by, int index)
{
     WebElement element = driver.findElement(By.id(Value));
     List<WebElement> elements = element.findElements(By.tagName("a")); 
     return elements.get(index);
}
driver.get(“www.xyz.com”);
WebElement=driver.findElement(By.id(Value));
列表元素=元素.findElements(按.tagName(“a”));
int-sizeOfAllLinks=elements.size();
System.out.println(sizeOfAllLinks);

对于(int i=0;i当您更改页面时,您将丢失对该元素的引用,因为它将不再位于DOM中。解决此问题的简单方法是在返回页面后始终再次获取锚定。基本上获取锚定的数量并执行一段时间。在此期间,始终获取锚定列表,并从该列表中获取所需的锚定。

W当您更改页面时,您将丢失对该元素的引用,因为它将不再位于DOM中。解决此问题的一个简单方法是在返回页面后始终再次获取锚定。基本上获取锚定的数量并执行一段时间。在此期间,始终获取锚定列表,并从该列表中获取所需的锚定。

WebDriver webDriver=新的FirefoxDriver();
        WebDriver webDriver=new FirefoxDriver();
            webDriver.get(SEARCH_URL);
            WebElement form2=webDriver.findElement(By.id("frmMain"));
            form2.submit();
            System.out.println(webDriver.getCurrentUrl());
            List<WebElement>doctorAnchors=webDriver.findElements(By.xpath("//td[@class='data']/a"));
            boolean x = false;
            int c = doctorAnchors.size();
            for(int i=0; i<c; I++){
                 if(x){

List<WebElement>doctorAnchors=webDriver.findElements(By.xpath("//td[@class='data']/a"));
                  }
            curElement = doctorAnchors.get(i);

            curElement .click();
            System.out.println((count++)+" : "+webDriver.getPageSource().toString());
            Thread.sleep(10000);
            webDriver.navigate().back();
            x = true;
            doctorAnchors = new ArrayList<WebElement>();
             }
webDriver.get(搜索URL); WebElement form2=webDriver.findElement(By.id(“frmMain”); 表格2.提交(); System.out.println(webDriver.getCurrentUrl()); ListdoctorAnchors=webDriver.findElements(By.xpath(“//td[@class='data']/a”); 布尔x=假; int c=doctorAnchors.size(); 对于(inti=0;i
WebDriver WebDriver=newfirefoxdriver();
webDriver.get(搜索URL);
WebElement form2=webDriver.findElement(By.id(“frmMain”);
表格2.提交();
System.out.println(webDriver.getCurrentUrl());
ListdoctorAnchors=webDriver.findElements(By.xpath(“//td[@class='data']/a”);
布尔x=假;
int c=doctorAnchors.size();

对于(int i=0;i@rahulserver,他在哪里使用多个webdriver实例?我只看到一个。请注意,这只是代码的一部分[我指的是片段];通常你声明为[私人webdriver;],每个人都知道这一点。我以为你不是新手;@Arran“他不应该期望你为他做他的工作。”给出一个更准确的代码几乎不需要几分钟。答案的质量就是如此。一个有这样小错误的代码不能被认为是一个完美的答案,即使它可能会给出解决方案的提示。@rahulserver,有时你会得到完美的答案,有时你不会,但你应该能够看到我所说的这是错误的。他的代码没有错,只是不完整。他认为你会看到不存在的内容,并将你自己的添加到其中。但是,很明显,你打算复制和粘贴so的答案用于你的工作,而这不是so的目的。这也是为了帮助你理解问题,帮助你解决问题,而不是解决问题为你解决问题。当然@rahulserver你明白了;我也同意Arran。让我们在这一点上妥协:)@rahulserver,他在哪里使用多个webdriver实例?我只看到一个。请注意这只是代码的一部分[我指的是片段];通常你声明为[私有webdriver;]每个人都知道这一点。我以为你不是新手;)@Arran“他不应该指望你为他工作。”给出一个更准确的代码几乎不需要几分钟。答案的质量就是如此。一个有这样小错误的代码不能被认为是一个完美的答案,即使它可能会给出解决方案的提示。@rahulserver,有时你会得到完美的答案,有时你不会,但你应该能够看到我所说的这是错误的。他的代码没有错,只是不完整。他认为你会看到不存在的内容,并将你自己的添加到其中。但是,很明显,你打算复制和粘贴so的答案用于你的工作,而这不是so的目的。这也是为了帮助你理解问题,帮助你解决问题,而不是解决问题为你解决问题。当然@rahulserver你说到点子上了;我也同意Arran。让我们在这一点上妥协:)