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

前端之家收集整理的这篇文章主要介绍了如何使用selenium webdriver在perl的警报/提示/对话框中获取文本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在下面的图片中找到一个字符串.它位于警报,提示或对话框内.我的代码是用Perl编写的,我使用Selenium Webdriver来浏览页面.

Image shows the alert and the Url I want from it

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

>找到硒的链接并点击它
>等待警报出现
>从警报中获取字符串,但不从文本字段中获取字符串

my $copy_elem = wait_until {      
    $d->find_element_by_id('clipboard-link'); 
};        
$copy_elem->click;

select undef,undef,8.00;

my $alert = wait_until {
  $d->get_alert_text;
};

$alert输出是“复制链接

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

CPAN Selenium Webdriver Docu with list of functions

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

解决方法

一种方法是使用脚本注入覆盖页面中的 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,8.00;

# get the prompt default input
my $input = $d->execute_script('return window.last_prompt.input;');

猜你在找的Perl相关文章