dongmei2956 2013-11-18 16:19
浏览 57
已采纳

Selenium webdriver循环错误:ObsoleteElementWebDriverError

I'm currently creating a unit test to be implemented in a website using Selenium and Facebook's php-webdriver. I am trying to go through all hyperlinks and verify if they work correctly using the following code:

$this->webDriver->get($this->url);
    $results = $this->webDriver->findElements(WebDriverBy::cssSelector('a'));

    for($i = 0; $i < count($results); $i++)
    {
        $this->webDriver->get($results[$i]->getAttribute('href'));
        echo $this->webDriver->getTitle() . "
";
        $this->webDriver->navigate()->back();
    }

The problem that occurs is that after the first "get" the unit test exits with the following error:

ObsoleteElementWebDriverError on
#4 C:\xampp\htdocs\php-webdriver-demo-master\tests\GitHubTest.php(63): RemoteWebElement->getAttribute('href')
#5 [internal function]: GitHubTest->testSearch()

When I don't switch from page, for example, when I just echo the title throughout the whole loop it loops perfectly. But as soon as I add an action it stops working. I've tried implementing "wait" but without any luck, it still throws the same error. Also, the code works perfectly when just running it once and not looping.. I can't seem to figure it out and have been crunching for a few hours now.. do you have any ideas?

  • 写回答

1条回答 默认 最新

  • douzhi9921 2013-11-19 04:38
    关注

    You can persist hrefs before loading other pages.

    $this->webDriver->get($this->url);
    
    $results = $this->webDriver->findElements(WebDriverBy::cssSelector('a'));
    $hrefs = array();
    for($i = 0; $i < count($results); $i++)
    {
        $hrefs[] = $results[$i]->getAttribute('href');
    }
    
    for($i = 0; $i < count($hrefs); $i++)
    {
        $this->webDriver->get($hrefs[$i]);
        echo $this->webDriver->getTitle() . "
    ";
        $this->webDriver->navigate()->back();
    }
    

    Moreover, it is not necessary to use navigate()->back();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog