selenium - 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_links
160 多个元素,但find_child_element()
仅适用于第一次迭代。
解决方案
А 页面加载几秒钟后,“cookies 警告”被添加到页面中(我之前没有注意到,当然,我很久以前在浏览器中手动接受了它)......我猜,使之前发生的一切无效。所以,我只是在之后添加了一个等待,get()
现在一切都很好。
推荐阅读
- php - 如何从源代码中提取函数调用中的参数?
- html - 需要 Reactjs 路由格式
- c# - 如何在 EVAL Listview 上使用 IF 语句
- ssl - 使用 WinInet 的客户端身份验证(证书 + 私钥)
- python - 如何获得图像的主要颜色?
- javascript - jquery-modal 中的 Javascript 不起作用 - Laravel
- python - 为什么 `is` 运算符在脚本和 REPL 中的行为不同?
- javascript - 在 Express 中间件中丢失 ajax 数据
- python - 任何具有相关模型的基于类的视图或 Auth 包?
- android - 将 PagedList 与 Google 地图一起使用