如何使用selenium webdriver在perl的警报/提示/对话框中获取文本?

Sai*_*aVA 4 perl selenium alert phantomjs selenium-webdriver

我想在下面的图片中找到一个字符串.它位于警报,提示或对话框内.我的代码是用Perl编写的,我使用Selenium Webdriver来浏览页面.

图像显示警报和我想要的Url

到目前为止我取得的成就:

  • 找到与selenium的链接并单击它
  • 等待警报出现
  • 从警报中获取字符串,但不从文本字段中获取字符串

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;
};
Run Code Online (Sandbox Code Playgroud)

$alert 输出是"复制链接"

所以我想要的文本在alert的文本字段中.使用get_alert_text我只获取Alert字符串,但不获取文本字段内容.我在网上搜索了答案,看到人们使用窗口句柄切换到警报.我试图在Selenium Webdriver的文档中寻找类似的功能:

CPAN Selenium Webdriver Docu功能列表

我尝试获取窗口句柄并将它们加载到一个数组中,但它没有获得警报的第二个窗口句柄.get_current_window_handle也不起作用.我使用phantomjs和chrome作为浏览器.据我所知driver.switchto().alert();,perl 是没有的.

Flo*_* B. 5

一种方法是使用脚本注入覆盖页面中的提示功能:

# 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;');
Run Code Online (Sandbox Code Playgroud)