How to check for pop up alert using selenium in python

2024/10/12 10:19:29

What I want is to continue with the next iteration if there is a pop up message in the webpage being scrapped. That is if there is any pop up message, I want to accept that message and go to the next item i.e go to the beginning of the loop.

For this I use the following snippet of code:

from tkinter import *
from tkinter import messagebox as msg
from tkinter import filedialog as fd
from tkinter import ttk
from tkinter import StringVar as sv
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutExceptionfor(i in range(0,5)):try:click_alert=driver.switch_to_alert()click_alert.accept()continueexcept TimeoutException:print('wrong value in'+i+'th row . Please check the value ') 

The following error shows up:

Exception in Tkinter callback

Traceback (most recent call last):File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__return self.func(*args)File "C:/Users/chowdhuryr/Desktop/ATT RESEARCH BOT/GUI.py", line 64, in printMessageself.Scrapper(str1,str2)File "C:/Users/chowdhuryr/Desktop/ATT RESEARCH BOT/GUI.py", line 163, in Scrapperclick_alert=driver.switchTo().alert()

Now I am pretty certain that the error lies in click_alert=driver.switch_to_alert() because I have checked it using some sanity checks.

Answer
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutExceptionbrowser = webdriver.Firefox()
browser.get("url")
browser.find_the_element_by_id("add_button").click()try:WebDriverWait(browser, 3).until(EC.alert_is_present(),'Timed out waiting for PA creation ' +'confirmation popup to appear.')alert = browser.switch_to.alertalert.accept()print("alert accepted")
except TimeoutException:print("no alert")
https://en.xdnf.cn/q/118212.html

Related Q&A

Rally host is non-existent or unreachable via pyral

I am trying to call rally server simply using below: rally = Rally(server, user, password, workspace=workspace, project=project)But it is giving below error:Traceback (most recent call last):File "…

Query tangled array in Pymongo

I am trying to query a very tangled collection. The schema:{tags: {variables: [{value: 3x9, var_name: s},{value: 12:00AM, var_name: x},{value: goog, var_name: y}]},url: https://www.google.com}]The Quer…

manipulating value of pandas dataframe cell based on value in previous row without iteration

I have a pandas dataframe with~3900 rows and 6 columns compiled from Google Finance . One of these columns defines a time in unix format, specifically defining a time during the trading day for a marke…

Convert ctypes code to cython

Id like to convert some ctypes code to use cython instead, but Im struggling. Essentially, the ctypes code:copies the contents (floats) of two lists into C-compatible structs sends the structs to my b…

Enable PyROOT Ubuntu 14.04

I downloaded madpgraph5, but when I run it I get the following error:ERROR: ROOT file called ROOT.py or ROOT.pyc is not foundERROR: Please check that ROOT is properly installed.When I try locate ROOT.p…

pygal on windows - cannot access classes from pygal

I have such short script:import pygal if __name__ == __main__:bar_chart = pygal.Bar()and following error: AttributeError: module object has no attribute BarDo you have any idea what is wrong? Shall I …

Parsing table for a link

Ive been able to isolate a row in a html table using Beautiful Soup in Python 2.7. Been a learning experience, but happy to get that far. Unfortunately Im a bit stuck on this next bit.I need to get t…

build matrix from blocks

I have an object which is described by two quantities, A and B (in real case they can be more than two). Objects are correlated depending on the value of A and B. In particular I know the correlation m…

How to convert string with UTC offset

I have date as In [1]: a = "Sun 10 May 2015 13:34:36 -0700"When I try to convert it using strptime, its giving error.In [3]: datetime.strptime(a, "%a %d %b %Y %H:%M:%S %Z"...: ) ---…

regex multiline not working on repeated patterns

I am trying to get a regex expression to match multiple patterns with multilines but it keeps matching everything. For instance I want to match two times this code:STDMETHOD(MyFunc)( D2D1_SIZE_U size, …