Home > Software engineering >  how can i chrome popup confirmation in python selenium?
how can i chrome popup confirmation in python selenium?

Time:01-05

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

Chrome Popup

how can i click "ok" in selenium?

CodePudding user response:

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

driver.switch_to.alert.accept()

CodePudding user response:

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

CodePudding user response:

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()
  • Related