bymechul
bymechul

Reputation: 87

how can i chrome popup confirmation in python selenium?

i use python and i want click "ok" button in Chrome selenium.

Chrome Popup

how can i click "ok" in selenium?

Upvotes: 2

Views: 1195

Answers (3)

Ronin
Ronin

Reputation: 2288

Updated answer with testing on PC

from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("http://testautomationpractice.blogspot.com/")

driver.find_element(By.XPATH, "//*[@id='HTML9']/div[1]/button").click()
sleep(5)
driver.switch_to.alert.dismiss() #  or .accept()

Upvotes: 1

Raie
Raie

Reputation: 81

The .alert.accept() some others are mentioning should work, but from my experience it didnt work on the applications I ran.

These two ways worked for me:

  1. since its a pop up and the focus is on the button you need you can simply use OS.system or subprocess library command 'enter' on the keyboard.
  2. this is a more tedious way. If you are on linux and are fine with adding mouse clicks, use the library xdotool find the coordinates on the selenium window use it to mouse click in that spot after a few seconds lag

links: subprocess: https://docs.python.org/3/library/subprocess.html xdotool: https://manpages.ubuntu.com/manpages/trusty/man1/xdotool.1.html

Upvotes: 1

Prophet
Prophet

Reputation: 33361

Accepting alert with Selenium in Python can be done with this:

driver.switch_to.alert.accept()

Upvotes: 2

Related Questions