无法单击使用findElements方法获得的链接

时间:2019-02-18 11:45:58

标签: selenium selenium-webdriver

这只是一个简单的代码,可以从网页的特定部分获取所有链接;单击这些链接即可获得标题,然后返回到父页面,并为下一个页面重复该过程。但是,单击2个链接后,出现错误“陈旧元素引用:元素未附加到页面文档中”。请参考代码,让我知道我所缺少的内容。

我能够在新标签中打开链接并获得子页面的标题。但是我想在同一标签中打开一个链接。

package test;

import java.util.List;

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

public class LinkTest {

    public static void main (String[] args) throws InterruptedException{
        System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\1\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.qaclickacademy.com/practice.php");

        WebElement driverFooter = driver.findElement(By.xpath("//div[@id='gf-BIG']"));
        WebElement footerC1 = driverFooter.findElement(By.xpath("//table/tbody/tr/td/ul[1]"));
        List<WebElement> links = footerC1.findElements(By.tagName("a"));


        for(int i=0;i<links.size();i++){
            links.get(i).click();
            System.out.println(driver.getTitle());
            driver.navigate().back();


        }

    }
}

它应该打开所有链接并获得标题,并返回到父页面以进行下一个迭代器。下面是输出和错误,

Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
    [1550488224.725][WARNING]: Timed out connecting to Chrome, retrying...
    Practice Page
    REST API Tutorial
    Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
      (Session info: chrome=72.0.3626.109)
      (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 6 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
    Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
    System info: host: 'DS-SGH631RNL8', ip: '10.118.59.16', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{mobileEmulationEnabled=false, timeouts={implicit=0, pageLoad=300000, script=30000}, hasTouchScreen=false, platform=XP, acceptSslCerts=false, goog:chromeOptions={debuggerAddress=localhost:53075}, acceptInsecureCerts=false, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=ignore, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1), userDataDir=C:\Users\RUSHIK~1.KAD\AppData\Local\Temp\scoped_dir11700_2428}, takesHeapSnapshot=true, pageLoadStrategy=normal, strictFileInteractability=false, databaseEnabled=false, handlesAlerts=true, version=72.0.3626.109, browserConnectionEnabled=false, proxy={}, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]
    Session ID: e1a6bdd0b525d1c6e1d940a2ec156e51
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
        at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
        at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
        at test.LinkTest.main(LinkTest.java:23)

1 个答案:

答案 0 :(得分:2)

您将收到StaleElementReferenceException,当您尝试访问的元素在dom中不再可用或变得陈旧时,会出现StaleElementReferenceException,要更正它,您需要再次选择元素然后对其进行操作。您可以修改代码,例如:

System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\1\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.qaclickacademy.com/practice.php");

WebElement driverFooter = driver.findElement(By.xpath("//div[@id='gf-BIG']"));
WebElement footerC1 = driverFooter.findElement(By.xpath("//table/tbody/tr/td/ul[1]"));
List<WebElement> links = footerC1.findElements(By.tagName("a"));

for(int i=0;i<links.size();i++){
driverFooter = driver.findElement(By.xpath("//div[@id='gf-BIG']"));
footerC1 = driverFooter.findElement(By.xpath("//table/tbody/tr/td/ul[1]"));
    List<WebElement> newLinks = footerC1.findElements(By.tagName("a"));
    newLinks.get(i).click();
    System.out.println(driver.getTitle());
    driver.navigate().back();
    driver.navigate().refresh();
}