首页 > 解决方案 > Selenium::Remote::Driver - 过时的元素引用:元素未附加到页面文档

问题描述

编辑:页面加载几秒钟后,cookie 警告被添加到页面中(我之前没有注意到,当然我很久以前在浏览器中手动接受了它)......我猜这会使任何内容无效在那之前发生过。所以我只是在 get() 之后添加了等待,现在一切都很好。

stale element reference: element is not attached to the page document循环遍历在find_elements()第一个元素之后找到的 WebElements 时出错。


尝试运行这个简单的脚本

use strict;
use warnings;
use feature qw/say/;

use Selenium::Chrome;
my $driver = Selenium::Chrome->new( binary => 'C:/path-to/chromedriver.exe' );

$driver->get( 'https://...' );

my $pdf_links = $driver->find_elements('//a[@class="file-link"]');

for my $link (@$pdf_links) {
    my $filename = $driver->find_child_element($link, "./span")->get_text();
    say $filename;
}

我得到这个错误:

first-file-name.pdf
Error while executing command: stale element reference: element is not attached to the page document
  (Session info: chrome=75.0.3770.100)
  (Driver info: chromedriver=75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770@{#1003})
,platform=Windows NT 10.0.18362 x86_64) at C:/<path>/perl/perl/site/lib/Selenium/Remote/Driver.pm line 403.

 at C:/<path>/perl/perl/site/lib/Selenium/Remote/Driver.pm line 353.

@$pdf_links160 多个元素,但find_child_element()仅适用于第一次迭代。

标签: seleniumperl

解决方案


А 页面加载几秒钟后,“cookies 警告”被添加到页面中(我之前没有注意到,当然,我很久以前在浏览器中手动接受了它)......我猜,使之前发生的一切无效。所以,我只是在之后添加了一个等待,get()现在一切都很好。


推荐阅读