Switch to popup in python using selenium

2024/10/2 22:27:07

How do I switch to a popup window in the below selenium program. I've looked up for all possible solutions but haven't been able to get my head around them. Please help!!

from selenium import webdriver
from splinter import Browser
from selenium.webdriver.common.keys import Keys
handle = []
driver = webdriver.Firefox()
driver.get("http://example.com/test.aspx")driver.find_element_by_link_text("Site Actions").click()
driver.find_element_by_link_text('Edit Page').click()
select = driver.find_element_by_id('ctl00_PlaceHolderMain_ctl35_ctl00_SelectResult')
for option in select.find_elements_by_xpath('//*[@id="ctl00_PlaceHolderMain_ctl35_ctl00_SelectResult"]/option'):if option.text != 'Channel':option.select() # select() in earlier versions of webdriverdriver.find_element_by_id('ctl00_PlaceHolderMain_ctl35_ctl00_RemoveButton').click()parent_h = driver.current_window_handle#click that activates the popup.
checkIn = driver.find_element_by_id('qaCheckin_anchor').click()# click on the link that opens a new window
handles = driver.window_handles # before the pop-up window closes
driver.remove(parent_h)
driver.switch_to_window(handles.pop())
driver.implicitly_wait(10) # seconds
driver.find_element_by_xpath('/html/body/form/div[3]/table/tbody/tr[4]/td/table/tbody/tr[3]/td[2]/input').click()
driver.find_element_by_name('btnClose2').click()
driver.close();
# do stuff in the popup
# popup window closes
driver.switch_to_window(parent_h)
# and you're backdriver.switch_to_default_content()
Answer

in terms of browser, pop up is not a window, it is an alert. so, you should use following:

driver.switch_to_alert()
https://en.xdnf.cn/q/70796.html

Related Q&A

Segmentation fault while redirecting sys.stdout to Tkinter.Text widget

Im in the process of building a GUI-based application with Python/Tkinter that builds on top of the existing Python bdb module. In this application, I want to silence all stdout/stderr from the consol…

Why do pandas and dask perform better when importing from CSV compared to HDF5?

I am working with a system that currently operates with large (>5GB) .csv files. To increase performance, I am testing (A) different methods to create dataframes from disk (pandas VS dask) as well a…

is there any pool for ThreadingMixIn and ForkingMixIn for SocketServer?

I was trying to make an http proxy using BaseHttpServer which is based on SocketServer which got 2 asynchronous Mixins (ThreadingMixIn and ForkingMixIn)the problem with those two that they work on each…

Python - Read data from netCDF file with time as seconds since beginning of measurement

I need to extract values from a netCDf file. I am pretty new to python and even newer this file format. I need to extract time series data at a specific location (lat, lon). I have found that there is …

PyQt Multiline Text Input Box

I am working with PyQt and am attempting to build a multiline text input box for users. However, when I run the code below, I get a box that only allows for a single line of text to be entered. How to …

Calculate the sum of model properties in Django

I have a model Order which has a property that calculates an order_total based on OrderItems linked by foreign key.I would like to calculate the sum of a number of Order instances order_total propertie…

Set Host-header when using Python and urllib2

Im using my own resolver and would like to use urllib2 to just connect to the IP (no resolving in urllib2) and I would like set the HTTP Host-header myself. But urllib2 is just ignoring my Host-header:…

Full-featured date and time library

Im wondering if anyone knows of a good date and time library that has correctly-implemented features like the following:Microsecond resolution Daylight savings Example: it knows that 2:30am did not exi…

Mean of a correlation matrix - pandas data fram

I have a large correlation matrix in a pandas python DataFrame: df (342, 342).How do I take the mean, sd, etc. of all of the numbers in the upper triangle not including the 1s along the diagonal?Thank…

How to set imshow scale

Im fed up with matplotlib in that its so hard to plot images in specified size.Ive two images in 32*32, 20*20 sizes. I just want to plot them in its original size, or in proportion to its original size…