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();

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

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1