SaikaVA SaikaVA - 3 months ago 50
Perl Question

How do I get the text in an alert/prompt/dialog in perl with selenium webdriver?

I want to get a string in the picture below. It is inside of an alert, prompt or dialog. My code is written in Perl and I am using Selenium Webdriver to navigate through the page.

Image shows the alert and the Url I want from it

What I have achieved so far:

  • finding the link with selenium and clicking on it

  • waiting for the alert to appear

  • get a string from the alert, but not the string in the text field


my $copy_elem = wait_until {

select undef, undef, undef, 8.00;

my $alert = wait_until {

output is "Copy Link"

So the text that I want is inside the alert's text field. With get_alert_text I only get the Alert string, but not the text field content. I searched the Web for answers and saw people using window handles to switch to the alert. I tried to look for similar functions in Selenium Webdriver's documentation:

CPAN Selenium Webdriver Docu with list of functions

I tried getting the window handles and load them into an array, but it does not get a second window handle for the alert. get_current_window_handle doesn't work either. I used phantomjs and chrome as browsers. As far as I know there is no
for perl.

Answer Source

One way is to override the prompt function in the page with a script injection:

# 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;');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download