How to click on open application alert using Selenium - python

I am trying to click on open application alert using Selenium, and I am getting this error
NoAlertPresentException: Message: no such alert
So basically I am trying to open zoom application from the browser
And here is my code:
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 TimeoutException
driver = webdriver.Chrome(executable_path='chromedriver/chromedriver')
driver.get("https://zoom.us/j/93459172503?pwd=QkhnMEQ0ZTRZd0grUVJkT2NudmlFZz09")
try:
WebDriverWait(driver, 5).until(EC.alert_is_present(), 'Timed out waiting for alerts to appear')
alert = driver.switch_to.alert
alert.accept()
print("alert accepted")
except TimeoutException:
print("no alert")

Thanks to NetworkMeister, I end up using default app method, Like the following:
1- Open Firefox and go to zoom URL, when Launch application appear choose zoom and click on remember my choice and click open link
2- Go to firefox and paste this about:support then search for Profile Folder and copy the path
3- Go to your code and add profile parameter to selenium driver and use the path you have copied
fp = webdriver.FirefoxProfile('C:/Users/ASUS//AppData/Roaming/Mozilla/Firefox/Profiles/0rgewd47.default-release')
driver = webdriver.Firefox(executable_path='geckodriver', firefox_profile=fp)
driver.get('https://zoom.us/j/93459172503?pwd=QkhnMEQ0ZTRZd0grUVJkT2NudmlFZz09')
Now zoom application will open automatically when you execute this code
Note: Firefox profile contains all of your browsing data, If you want to share your code I suggest creating a new Firefox profile, For more information look here

Because this is not a browser Alert, rather OS App selector, you cannot interact with it within Selenium.
See: Selenium C# How to handle Alert "Open Pick an app"?
You can prevent these App selectors by default by using the --disable-default-apps flag when starting up Chrome.

I encountered this problem my self.
Apparently this is related to the alert being OS level rather than being at a browser level.
The simplest solution I found is https://pypi.org/project/PyAutoGUI/ which allows you to pass an img of the button you want to click and then it locates it on the screen. You can call python directly from java.
Native java solution: https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
this allows you to automate stuff like "move mouse to position" -> "mouse click" etc.
EDIT: the main downside is that the webdriver cannot be run in headless mode as PyAutoGUI makes a screenshot of the screen to locate the button.

From what I noticed is that when you open any such link in the browser the URL changes a bit. With this you can figure out the link for opening it in the browser. For example I have this zoom meeting
https://us04web.zoom.us/ w/ 76919011107?tk=5Q_zikLZhvWlqc_nzVcYaHoTyo7JuDY6cvLB9y9t0zc.DQIAAAARDLr3IxZHM21mSHFzYlR6Q0xZdnNhcnUwbUV3AAAAAAAAAAAAAAAAAAAAAAAAAAAA&pwd=QANkcTdsNlpjYWY4czZvd3FHV0NLQT09
Now this would open a popup like you showed in the browser. (This link is not real. I changed 2-3 characters in the URL for security reasons). But now if you try this link
https://us04web.zoom.us/ wc/join/ 76919011107?tk=5Q_zikLZhvWlqc_nzVcYaHoTyo7JuDY6cvLB9y9t0zc.DQIAAAARDLr3IxZHM21mSHFzYlR6Q0xZdnNhcnUwbUV3AAAAAAAAAAAAAAAAAAAAAAAAAAAA&pwd=QANkcTdsNlpjYWY4czZvd3FHV0NLQT09
This will directly open the meeting in the browser. Note the change in URL has been shown in bold (I had to add spaces because of that on both sides). Now this may change over time but fundamentally by comparing the 2 links you should find a way to achieve this.
Similarly for Microsoft Teams if you add "_#" after the domain you can join the meeting through the browser
For example if I want to open this link in the browser
https://teams.microsoft.com/l/meetup-join/19%3ameeting_NTRlM2JiZWUtZjNiMC00ZjVhLTlmMWEtZDcxYjBmYjdhY2Nl%40thread.v2/0?context=%7b%22Tid%22%3a%22e85f2c00-2730-4ca5-b8d8-609b15bd4746%22%2c%22Oid%22%3a%223bf1c992-96b1-4a87-808f-dcc5bb2009c9%22%7d
I'll have to write
https://teams.microsoft.com/_#/l/meetup-join/19%3ameeting_NTRlM2JiZWUtZjNiMC00ZjVhLTlmMWEtZDcxYjBmYjdhY2Nl%40thread.v2/0?context=%7b%22Tid%22%3a%22e85f2c00-2730-4ca5-b8d8-609b15bd4746%22%2c%22Oid%22%3a%223bf1c992-96b1-4a87-808f-dcc5bb2009c9%22%7d
These patterns can be found by observing the link before and after opening it in the browser.

This is not the best way but I resolved the issue by sending keyboard commands. It works on Windows, I haven't tested on different os.
import pyautogui
sleep(3) # sleep until pop up shown
pyautogui.press('tab', presses=2) # navigate to open button
pyautogui.press('enter') # open application

For me using Robot class worked for teams.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);

Related

Is there any way to send commands (like ":screenshot") to the firefox console with Selenium using Python?

I am working on an automation task that opens a webpage, screenshots an element from the page, then closes the page and moves on to the next. I had previously set this up using AutoHotKey and, although it worked technically, I wanted to create a more refined version. Selenium has worked well up to now for automating the navigation of pages, but when I would be ready to take my screenshot, I can't seem to get the console open to issue the command. I tried using the
from selenium.webdriver.common.keys import Keys
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.SHIFT + 'K')
command but it seems that firefox doesn't receive the input. I also tried
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(browser)
actions.send_keys(Keys.CONTROL + Keys.SHIFT + 'k')
which, again, didn't seem to work.
Lastly I tried using
browser.execute_script(":screenshot")
but I kept getting a JavaScript error which makes sense since the screenshot command isn't js.
If there's anything you can think of that I am overlooking please let me know! Thanks in advance :)
Selenium screenshot
Example
You can take a screenshot of a webpage with the method get_screenshot_as_file() with as parameter the filename.
The program below uses firefox to load a webpage and take a screenshot, but any web browser will do.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get('https://www.python.org')
sleep(1)
driver.get_screenshot_as_file("screenshot.png")
driver.quit()
print("end...")
The screenshot image will be stored in the same directory as your Python script. Unless you explicitly define the path where the screenshot has to be stored.

How to Navigate Context Menus (Selenium, Python)

I am aware that Selenium apparently doesn't support navigating context menus. But I have also seen in several other threads that there is a work-around by using action chains. Using context_click() followed by arrow key commands to navigate through the menus.
All examples I've seen have used Java, and when I translated to Python, only the context_click() command would register. Strangely enough, I wouldn't get an error either. Other sources have said that the context menus Selenium produces are only system level, and thus, Selenium cannot touch them, only create.
So my question is, has anyone been able to successfully navigate and chose options from context menus through Selenium? Python examples are preferred, but I'll take any advice or answers I can get.
Edit:
Code:
driver.get('https://www.google.com/')
actionChains = ActionChains(driver)
actionChains.context_click().send_keys(Keys.ARROW_UP).send_keys(Keys.ENTER).perform()
Context:
This is just a test script that I have been running to test this situation. In my personal project, I need to navigate the context menu to access a chrome extension. Since selenium can only interact within the web page I can't have it click on the button for the Chrome extension that is displayed by the browser. So this is the work-around I have been attempting.
Research:
https://testingrepository.com/how-to-right-click-using-selenium-webdriver/
- This source tells that seleniums context menus are only system level. In Java examples they also use a .build() command. As far as my knowledge, this command is not available to Python.
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
- Thread suggesting that arrow key commands should work. However, all examples use Java and the .build() command as well
https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/common/action_chains.py
- shows that ActionChains() are Pythons's version of a .build() command. Might be common knowledge to some. I did not know this prior.
How to perform right click using Selenium ChromeDriver?
- very similar question to mine. While one user suggests that the menu cannot be interacted with, another suggests the actionChains work-around will work.
Thin,
I had the same problem and wonder nobody answered this already... It wasn't possible for me to solve it with selenium, cause selenium would navigate within the page. My solution:
import win32com.client as comclt
wsh= comclt.Dispatch("WScript.Shell")
ActionChains(driver).move_to_element(element).context_click().perform()
wsh.SendKeys("{DOWN}") # send the keys you want
Well we can solve this using selenium along with pyautogui. The reason for using pyautogui is that we need to have control of the mouse for controlling the options on the context menu. To demonstrate this, I am going to use a python code to automatically open a google image of Avengers Endgame in new tab.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import pyautogui
URL = 'https://www.google.com/'
PATH = r'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
action = ActionChains(driver)
driver.get(URL)
search = driver.find_element_by_name('q')
search.send_keys('Avengers Endgame')
search.send_keys(Keys.RETURN)
image_tab = driver.find_element_by_xpath('//a[text()="Images"]')
image_tab.click()
required_image = driver.find_element_by_xpath('//a[#class="wXeWr islib nfEiy mM5pbd"]')
action.context_click(required_image).perform()
pyautogui.moveTo(120, 130, duration=1)
pyautogui.leftClick()
time.sleep(1)
pyautogui.moveTo(300,40)
pyautogui.leftClick()
Now in the above code, the part till pyautogui.moveTo(120, 130, duration=1) is selenium based. Your answer begins from pyautogui.moveTo(120, 130, duration=1) and what this does is simply moves the mouse button to the open image in new tab option of the context menu(Please note that the screen coordinates may vary based on your screen size). The next line clicks the option(using action.click().perform() won't work as expected).
The next two lines helps in navigating to the tab after it opens. Hope the code helps!
one way to work around this I think at least in java is to perform those actions with one instance of the class Robot.
This is just an example of handling authentication in chromme. Is very usefull when I need to perform actions out the selenium scope.
try{
Robot bot = new Robot();
for(char c: userArray){
bot.keyPress(c);
bot.keyRelease(c);
bot.delay(300);
}
bot.keyPress(KeyEvent.VK_TAB);
bot.keyRelease(KeyEvent.VK_TAB);
for(char c: userArray){
bot.keyPress(c);
bot.keyRelease(c);
bot.delay(300);
}
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
}catch(Exception e){
e.printStackTrace();
}

Selenium + Python - After clicking on a button, how to I catch the pop up window (specifically, an upload image pop up)

I'm trying to upload images to Microsoft's http://how-old.net/ API to compare with our age and gender classification algorithm [CVPR AMFG15].
I'm using Selenium and it's pretty easy to navigate to the web-site and click the "Use your own photo" button:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://how-old.net/")
elem=driver.find_element_by_id("uploadFileId")
elem.click()
I tried various solution that I found online:
upload_window=driver.find_element_by_partial_link_text("File")
Or:
driver.SwitchTo().defaultContent();
Nothing seems to work. Again, finding the button and clicking it is very easy, catching the window and uploading an image seems the hard part.
EDIT:
I've also tried the following:
driver = webdriver.Firefox()
driver.get("http://how-old.net/")
file_input=driver.find_element_by_id("uploadBtn")
driver.execute_script("arguments[0].style.visibility='visible';", file_input)
file_input.send_keys('image_name.jpg')
But I get the following exception:
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
Even though from what I see, the button is visible (see attached print screen).
[CVPR AMFG15] Levi, Gil, and Tal Hassner. "Age and Gender Classification using Convolutional Neural Networks."
You cannot control the file upload window with selenium.
The common approach to solve the problem is to avoid it being opened in the first place.
Find the file input and pass the path to the file using send_keys():
file_input = driver.find_element_by_id("uploadBtn")
file_input.send_keys("/absolute/path/to/file")
This would not work "as is" since the file input is hidden, make it visible first:
driver.execute_script("arguments[0].style = {visibility: 'visible'};", file_input)

blocking pop-ups resulting from selenium firefox driver

I am attempting to scrape websites using selenium-python. I am trying to use firefox driver (since PhantomJS doesn't work on me), but is there way to block the pop-ups when using the firefox driver?
thank you
Even I turn on "disable pop ups" in FireFox manually, it's not working for all website. A work around is switch to pop up window, close it, and switch back to the original window.
Here is the code in python:
# Switch to new window opened
driver.switch_to.window(driver.window_handles[-1])
# Close the new window
driver.close()
# Switch back to original browser (first window)
driver.switch_to.window(driver.window_handles[0])
Sorry for the late answer, but you can use the dom.popup_maximum profile preference, setting it to 0 like this:
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.set_preference("dom.popup_maximum", 0)
driver = webdriver.Firefox(firefox_profile=fp)
url = "http://some.url.with.annoying.popups"
driver.get(url)
For a list of other available options, please refer to this answer
Use xvfb in the framebuffer and here is a simple usage.
It provides an X environment for selenium.

How to control Webpage dialog with python

When I try to automatically download a file from some webpage using Python,
I get Webpage Dialog window (I use IE). The window has two buttons, such as 'Continue' and 'Cancel'. I cannot figure out how to click on the Continue Button. The problem is
that I don't know how to control Webpage Dialog with Python. I tried to use
winGuiAuto to find the controls of the window, but it fails to recognize any Button type
controls... An ideas?
Sasha
A clarification of my question:
My purpose is to download stock data from a certain web site. I need to perform it for many stocks so I need python to do it for me in a repetitive way. This specific site exports the data by letting me download it in Excel file by clicking a link. However after clicking the link I get a Web Page dialog box asking me if I am sure that I want to download this file. This Web page dialog is my problem - it is not an html page and it is not a regular windows dialog box. It is something else and I cannot configure how to control it with python. It has two buttons and I need to click on one of them (i.e. Continue). It seems like it is a special kind of window implemented in IE. It is distinguished by its title which looks like this: Webpage Dialog -- Download blalblabla. If I click Continue mannually it opens a regular windows dialog box (open,save,cancel) which i know how to handle with winGuiAuto library. Tried to use this library for the Webpage Dialog window with no luck. Tried to recognize the buttons with Autoit Info tool -no luck either. In fact, maybe these are not buttons, but actually links, however I cannot see the links and there is no source code visible... What I need is someone to tell me what this Web page Dialog box is and how to control it with Python. That was my question.
You can't, and you don't want to. When you ask a question, try explaining what you are trying to achieve, and not just the task immediately before you. You are likely barking down the wrong path. There is some other way of doing what you are trying to do.
The title 'Webpage Dialog' suggests that is a Javascript-generated input box, hence why you can't access it via winGuiAuto. What you're asking directly is unlikely to be possible.
However, making the assumption that what you want to do is just download this data from the site, why are you using the GUI at all? Python provides everything you need to download files from the internet without controlling IE. The process you will want to follow is:
Download the host page
Find the url for your download in the page (if it changes)
Download the file from that url to a local file
In Python this would look something like this:
import urllib,re
f = urllib.urlopen('http://yoursitehere') # Original page where the download button is
html = f.read()
f.close()
m = re.search('/[\'"](.*\.xls)["\']/', html, re.S) # Find file ending .xls in page
if m:
urllib.urlretrieve(m.group(1), 'local_filename.xls') # Retrieve the Excel file
It is better to use selenium Python bindings:
from selenium import webdriver
from selenium.webdriver.common import alert
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
class AlertsManager:
def alertsManager(self,url):
self.url_to_visit=url
self.driver=webdriver.Ie()
self.driver.get(self.url_to_visit)
try:
while WebDriverWait(self.driver,1).until(EC.alert_is_present()):
self.alert=self.driver.switch_to_alert()
self.driver.switch_to_alert().accept()
except TimeoutException:
pass
if __name__=='__main__':
AM=AlertsManager()
url="http://htmlite.com/JS006.php" # This website has 2 popups
AM.alertsManager(url)

Categories

Resources