如何使用 selenium webdriver 在 perl 中获取 alert/prompt/dialog 中的文本?
How do I get the text in an alert/prompt/dialog in perl with selenium webdriver?
我想获取下图中的字符串。它位于警报、提示或对话框中。我的代码是用 Perl 编写的,我正在使用 Selenium Webdriver 浏览页面。
目前我取得的成就:
- 找到带有 selenium 的 link 并点击它
- 等待警报出现
- 从警报中获取字符串,但不是文本字段中的字符串
代码
my $copy_elem = wait_until {
$d->find_element_by_id('clipboard-link');
};
$copy_elem->click;
select undef, undef, undef, 8.00;
my $alert = wait_until {
$d->get_alert_text;
};
$alert
输出为 "Copy Link"
所以我想要的文本在警报的文本字段中。使用 get_alert_text 我只得到警报字符串,而不是文本字段内容。我在网上搜索答案,看到人们使用 window 句柄切换到警报。我试图在 Selenium Webdriver 的文档中寻找类似的功能:
CPAN Selenium Webdriver Docu with list of functions
我尝试获取 window 句柄并将它们加载到数组中,但它没有为警报获取第二个 window 句柄。 get_current_window_handle 也不行。我使用 phantomjs 和 chrome 作为浏览器。据我所知,perl 没有 driver.switchto().alert();
。
一种方法是用脚本注入覆盖页面中的 prompt 函数:
# override the prompt function
$d->execute_script('window.prompt=function(message, input){window.last_prompt = {message: message, input: input}};');
# trigger a prompt
select undef, undef, undef, 8.00;
# get the prompt default input
my $input = $d->execute_script('return window.last_prompt.input;');
我想获取下图中的字符串。它位于警报、提示或对话框中。我的代码是用 Perl 编写的,我正在使用 Selenium Webdriver 浏览页面。
目前我取得的成就:
- 找到带有 selenium 的 link 并点击它
- 等待警报出现
- 从警报中获取字符串,但不是文本字段中的字符串
代码
my $copy_elem = wait_until {
$d->find_element_by_id('clipboard-link');
};
$copy_elem->click;
select undef, undef, undef, 8.00;
my $alert = wait_until {
$d->get_alert_text;
};
$alert
输出为 "Copy Link"
所以我想要的文本在警报的文本字段中。使用 get_alert_text 我只得到警报字符串,而不是文本字段内容。我在网上搜索答案,看到人们使用 window 句柄切换到警报。我试图在 Selenium Webdriver 的文档中寻找类似的功能:
CPAN Selenium Webdriver Docu with list of functions
我尝试获取 window 句柄并将它们加载到数组中,但它没有为警报获取第二个 window 句柄。 get_current_window_handle 也不行。我使用 phantomjs 和 chrome 作为浏览器。据我所知,perl 没有 driver.switchto().alert();
。
一种方法是用脚本注入覆盖页面中的 prompt 函数:
# override the prompt function
$d->execute_script('window.prompt=function(message, input){window.last_prompt = {message: message, input: input}};');
# trigger a prompt
select undef, undef, undef, 8.00;
# get the prompt default input
my $input = $d->execute_script('return window.last_prompt.input;');