ABraslavskiy
ABraslavskiy

Reputation: 11

Get text from a prompt alert using python selenium webdriver

I'm trying to scrape this page: https://foundation.app/@bryanmarktaylor. The problem I have is how to get the number to the left of the copy to clipboard button below the avatar (0x068C...). When I'm using python selenium webdriver with Chrome, instead of copying the number to the clipboard, the browser shows a prompt alert that says Copy to clipboard: Ctrl+C, Enter and shows a textbox with the number I need.

I cannot inspect the code of the alert to catch the element. I've tried using

alert = driver.switch_to.alert
alert.send_keys(Keys.CONTROL, 'c')

But it gives me

TypeError: send_keys() takes 2 positional arguments but 3 were given

I've also tried using a JS script

driver.execute_script("""arguments[0].getAttribute("value");""", alert)

but it looks like I cannot send the alert element as an argument

TypeError: Object of type Alert is not JSON serializable

Upvotes: 1

Views: 437

Answers (1)

pguardiario
pguardiario

Reputation: 54984

You can redefine alert to save the message:

driver.execute_script('window.alert = text => window.alertText = text')
// do something that triggers the alert
alert_text = driver.execute_script('return window.alertText')

Upvotes: 1

Related Questions