How to get text in alert / prompt / dialog in perl with selenium webdriver?

I want to get the line in the image below. It is inside a warning, prompt or dialogue. My code is written in Perl, and I use Selenium Webdriver to navigate the page.

Image shows warning and url I want from it

What I have achieved so far:

  • find the link with selenium and click on it
  • Waiting for a warning.
  • get string from warning but not string in textbox

the code

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" is displayed

So the text I want is inside the alert text box. With get_alert_text, I get only the Alert string, but not the contents of the text field. I searched the Internet for answers and saw people using window handles to switch to a warning. I tried to find similar functions in the Selenium Webdriver documentation:

CPAN Selenium Webdriver Docu

, . get_current_window_handle . phantomjs chrome . , perl driver.switchto().alert();.

+4
1

- prompt 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;');
+5

Source: https://habr.com/ru/post/1689052/


All Articles