Using Selenium Webdriver in Python I am able to click on a button that opens a new browser window, but I have no idea how to change the focus onto the new window. I've searched all over the internet, but found nothing helpful.
The problem is that the window doesn't have a title!
What I need is the focus on the new window, so I can take a screenshot of its contents.
Here is the bit of code around the button that opens the new window:
How to do that...?
You will need to use the .switchTo(windowHandle); command to access your second window.
Before opening the second window - get the windowHandle of the open window:
String mainWindow = driver.getWindowHandle();
Then do your action that opens the second window. Now you'll need to know the handle of the second windowand switch control to it:
Set<string> handles = driver.getWindowHandles();
for (String handle : handles) {
if (!handler.equals(mainWindow)) {
driver.switchTo(handle);
break;
}
}
Your actions for the second window will now happen in that second window. When you're finished and need to interact with the first window again: driver.switchTo().defaultContent();
This worked for me in Python:
another_window = list(set(driver.window_handles) - {driver.current_window_handle})[0]
driver.switch_to.window(another_window);
Related
I'm using Python and Selenium to write an automation script in Internet Explorer.
When the web page throws up some kind of modal dialog box, the Python code stops running and just waits for some action to be taken on the popup. After you press the "yes" or "no" button, then the Python code continues.
I believe the underlying Javascript function that is getting called (saveClicked()) is generating the popup box using this line of code:
var result=window.showModalDialog('whatever....')
Does anyone know how to handle this in Selenium? I want my code to click "ok" in this window or to just accept it. I tried right-clicking on the window to look at source code, etc. but those options are not given to me...the only options are "move/close".
I've looked to see if there is some kind of default IE capability in Selenium that will just automatically accept all modal dialog boxes but haven't found any. I also thought of maybe wrapping the call to the Javascript function with something that would somehow send a keystroke to the alert. I'm open to anything!
Here is the code: It never moves past the .execute_script line...it just sits there waiting.
print('Saving')
# I have to do this because I can't get the handle to the save button
# using any of the known Selenium methods but calling the JS works
driver.execute_script('saveClicked();')
print('Test')
driver.switch_to().alert().send_keys(Keys.ENTER)
The code just STOPS after the Javascript is executed and never moves to the print('test') line or any other code I put there.
Any python selenium code suggestions to solve this would be greatly appreciated.
one way: you can try to use Alert to manage popups
Alert(driver).accept()
otherwise you can see the active window or tab with:
#get current window handle
p = driver.current_window_handle
#get windows
chwd = driver.window_handles
driver.switch_to.window(chwd[1])
reference:
https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.alert
Try:
message = "saveClicked()" # or any other of your messages
driver.execute_script(f"alert(\"{message}\");")
driver.switch_to.alert.accept()
I have a two like like below
<body>
contact
Sest
</body>
After click on link page has open in new tab , I want to close this tab without close driver, Is it possible ? I have tried below code but not working.
link = driver.find_element_by_xpath(
"//a[contains(text(),'contact') or contains(text(),'est')]")
link.click()
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
You can switch between windows by using driver.window_handles to identify all the windows currently open in your driver's browser. Then you can move between them using `driver.switch_to_window(). Here's an example:
tabs = driver.window_handles #get list of open windows
driver.switch_to_window(tabs[1])
driver.close() #close the current window
driver.switch_to_window(tabs[0]) #return to first window
A tab is nothing more than another window (from the eyes of selenium)
To close a window you must switch to it, then call driver.close(). This does NOT cause selenium to quit the driver itself, which I suspect is what you might be afraid of.
To switch to a window, you'll need the window ID.
driver.current_window_handle gives you the window you're currently focused. driver.window_handles will give you a list of all the windows open. Assuming you only have two windows open, it should be easy to identify the one you want to close.
Lastly, don't forget to switch back to the original window. Closing a window does not automatically move your focus back to a window.
driver.switch_to.window(window_id)
driver.close()
driver.switch_to.window(original_id)
It's like a real browser. If you close the one and only tab, the browser closes himself. Try not to open the second Tab in a new Tab, just use the other you don't want anymore.
I'm quite new in Python, and I'm creating a robot to export some reports from a system that is in Silverlight. Therefore, I can't use elements of the page to click/select/insert dates, etc.
Then, my solution was using pyautogui to move the mouse, click on the right buttons and save the files...
The process is:
-Select dates of the report
-Click on the "team" I need the report for
-Click on preview button (a new window opens here)
-Maximise this window which contains the report
-Click on 'Export' button (drop-down)
-Select 'Export to CSV'
And then, via code, get last downloaded file and move it into the folder where I save the reports.
The bottleneck on this is: sometimes, due to an unknown reason, 'Export' button is 20/'30 pixels aside. It means that the robot clicks in nowhere, and the code stops since there is no "last downloaded file" to handle.
I already noticed this second window (where the report opens) is not in Silverlight. It's simple HTML. I know I could use some methods in selenium to select the Export/Export to CSV option (similarly as I've done to login into this system). But my problem is: how do I refer to this new window?
The code I'm using refers to the main page using selenium.wedriver but I couldn't find a way to refer to the new window (that is closed after I downloaded the CSV. Then I click again in "Preview" and another window opens, and the process repeat hundreds of times)...
wsite='https://WEBSITE TO EXTRACT'
driver = webdriver.Ie('C:\\MyFolder\\IEDriverServer.exe')
driver.get(wsite) #opens the site
driver.find_element_by_id('userNameInput').send_keys('USERNAME')
driver.find_element_by_id ('passwordInput').send_keys('PASSWORD')
driver.find_element_by_id('submitButton').click()
Then, there are a lots of lines of code (where the mouse clicks where it's needed...
pyautogui.moveTo(1846,1000,intervaltoclick)#Preview button
pyautogui.click()
time.sleep(3)
hwnd = win32gui.GetForegroundWindow()
win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
if i<1:
time.sleep(10+t)
else:
time.sleep(5+(t/5))
pyautogui.moveTo(566,52,intervaltoclick)#Export
pyautogui.click()
pyautogui.moveTo(603,205,intervaltoclick)#Save as CSV
pyautogui.click()
time.sleep(3)
pyautogui.moveTo(1270,1025,intervaltoclick)#close window
pyautogui.click()
Would someone have any idea on how can I do that?
You can try to keep the selenium object of IE browser that you used for login. You can keep it hidden if you don't want to show on a screen. You can than try to loop through the IE windows and switch to desired window.
Set<String> allWindowHandles = browser.getWindowHandles();
for(String handle : allWindowHandles)
{
System.out.println("Window handle - > " + handle);
browser.switchTo().window(handle);
System.out.println(browser.getTitle());
}
Here is a helpful link which shows the examples to switch the window.
Selenium WebDriver Switch Window Commands
I am trying to automate some basic web browser manipulation. As part of this sequence there is a popup window that must be dealt with. Once the python code the causes the popup window to appear happens, no other code past that point will run until I manually close that window. Then all remaining code runs fine.
I've try this below to try to deal with handling the popup but to no avail:
for handle in browser.window_handles:
print "Handle = ",handle
browswer.switch_to_window(handle);
elem=browser.find_element_by_tag_name("title")
print elem.get_attrbitue("value")
I cannot even get a basic 'print' command to execute until I close the popup window. Is there a need to set some type of event handler BEFORE the code is called that brings the popup menu?
Thanks for any suggestions.
I am exporting my selenium test to python and running the test using the shell. I get to a certain point where I click on a link and the web-driver then opens a completely new Firefox window and then I get an error saying that the driver can't find the input option on the page. I think the problem is that when it opens the new window the driver is not running on the new window, which is why it can't find the input option.
How do I get the script to stay on the same window or switch it's focus to the new window?
Thanks!
I don't know how to do it in python, but there should be a function to switch to a new window.
In Java, I do this:
Set<String> availableWindows = driver.getWindowHandles();
for (String windowHandle : availableWindows) {
driver.switchTo().window(windowHandle);
if (getTitle().contains(TITLE_TO_MATCH)){
driver.manage().window().maximize();
return;
}
}
(The function want is driver.switchTo().window(NameOrHandle))