selenium How to dismiss a chrome popup - python

I tried anything I could find on this but I can't get selenium to dismiss this alert. I don't think it's an alert anymore though, as I get NoAlertPresentException when I try
driver.switch_to.alert.dismiss()
Any ideas?
Here are relevant parts of the HTML page:
<iframe id="teamsLauncher" src="msteams:/l/team/19:95f3ef22a37040fca83b679b64f96da1#thread.tacv2/conversations?groupId=09988212-e3a1-4187-b067-a805e512e0a4&tenantId=d348f9ab-4879-4a60-ab50-5c420b11470c&deeplinkId=2deee564-c6b3-4d5d-b097-93900dd14620&launchAgent=join_launcher&type=team&directDl=true&msLaunch=true&enableMobilePage=true&fqdn=teams.microsoft.com" style="display: none;"></iframe>
<script>var modernBrowser="fetch"in window&&"assign"in Object&&"Set"in window,bundles=[],polyfills="";function injectScript(n,e,s){var t=document.createElement("script");t.src=n,t.async=e,t.onload=s,document.body.appendChild(t)}function loadBundles(n){for(key in bundles)injectScript(bundles[key],n)}function loadPolyfills(){polyfills&&injectScript(polyfills,!1,function(){loadBundles(!1)})}bundles.push("https://statics.teams.microsoft.com/hashedjs-launcher/launcher.5c24f1e70f34f56835c7.js"),polyfills="https://statics.teams.microsoft.com/hashedjs-launcher/polyfills.1e46ca6473519cf1be5d.js",modernBrowser?loadBundles(!0):loadPolyfills(),window.addEventListener("load",function(){injectScript("https://az725175.vo.msecnd.net/scripts/jsll-4.js",!0,function(){var n={coreData:{appId:"JS:teams.microsoft.com",market:window.navigator.language||"en-US"}};awa&&awa.init&&awa.init(n)})})</script>
Also tried actionchains, but sending escape key doesn't do anything.

Hard to say without running the code, but maybe try disabling chrome popups with Chrome Options?
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-notifications")
driver = webdriver.Chrome(options=chrome_options)
I hope, this works.

Consider using the library pyAutoGUI, it allows python to control your mouse and keyboard. If you keep the window in the same place each time, a static x y coordinate for the mouse click function can be used to accept the popup and allow you to procced

Related

The way to show mouse cursor using selenium in python

I tried to click on chrome browser while using ActionChains.
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(URL)
link = driver.find_element_by_id('link')
action = ActionChains(driver)
action.double_click(advanced_options_link).perform()
But what is more?!
I want to see mouse cursor on browser to monitor what is exactly happening.
*** Please help ***
I needed to use a function like this in the past. as far as Iv'e researched I learned that it's not possible with selenium. you can hover on elements or even move the cursor to a specific location on the scree, but you can't see it moving because it's not really moving the cursor, selenium dose not have control over the cursor in that fashion.
You can try to research this, you might be able to use something outside selenium to control the actual cursor of the computer. but, even if it's possible it will still be very hard to get it to work reliably because you don't have the control on the website that you have with selenium so everything becomes manual, therefore hard and keen to generate errors.
Does it work if you try using the move_to_element() from ActionChains? something like this;
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get(URL)
link = driver.find_element_by_id('link')
hover = ActionChains(driver).move_to_element(link)
hover.perform()

How to switch between different chrome browser window opened by different WebDriver using selenium in Python?

I searched for this question,and I found an idea using driver.switch_to.window(),but it didn't work as expect:
from selenium import webdriver
driver1=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver1.get('https://www.google.com')
driver2=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver2.get('https://www.bing.com/')
driver1.switch_to.window(driver1.current_window_handle)
above code will first open a chrome window and go to google,then will open another chrome window and go to bing,then
driver1.switch_to.window(driver1.current_window_handle)
seems didn't work,the window showing bing still shows on top of the window showing google.
Anyone have any idea?I think
driver1.switch_to.window(driver1.current_window_handle)
may have some BUG.
As you have used two WebDriver instances as driver1 and driver2 respectively to openthe urls https://www.google.com (e.g. windowA) and https://www.bing.com/ (e.g. windowB) it is worth to mention that the function switch_to.window() is a WebDriver method. So, driver1 can control only windowA and driver2 can control only windowB.
For Selenium to interact with any of the Browsing Window, Selenium needs focus. So to iterate among the different Browsing Windows you can shift the focus to the different Browsing Window using JavascriptExecutor as follows :
Python:
driver1.execute_script("window.focus();")
driver2.execute_script("window.focus();")
Java:
((JavascriptExecutor) driver1).executeScript("window.focus();");
((JavascriptExecutor) driver2).executeScript("window.focus();");
I believe you have a different concept of "window" in driver.switch_to.window(). In chrome browser, it means "tab". It's not another chrome browser or browser window like what are you trying to do in your code.
If switch_to.window() what you really want, I'll give an example how to use it:
driver=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver.get('https://www.google.com')
# open a new tab with js
driver.execute_script("window.open('https://www.bing.com')")
driver.switch_to.window(driver.window_handles[-1])
# now your driver is pointed to the "tab" you just opened

Python 2.7 - Detect opened browser popup window [duplicate]

Is there a possibility if I code a program in python that allows to automatically browse a given website using mechanize to detect if there are popup windows (suggesting advertisements or downloading actions ...) using Python ?. I would appreciate any hint (for example, if you give me a library that fulfills this task I would be very happy)
Mechanize cannot handle javascript and popup windows:
How do I use Mechanize to process JavaScript?
Mechanize and Javascript
To accomplish the goal, you need to utilize a real browser, headless or not. This is where selenium would help. It has a built-in support for popup dialogs:
Selenium WebDriver has built-in support for handling popup dialog
boxes. After you’ve triggerd and action that would open a popup, you
can access the alert with the following:
alert = driver.switch_to_alert()
Example (using this jsfiddle):
from selenium import webdriver
url = "http://fiddle.jshell.net/ebkXh/show/"
driver = webdriver.Firefox()
driver.get(url)
button = driver.find_element_by_xpath('//button[#type="submit"]')
# dismiss
button.click()
driver.switch_to.alert.dismiss()
# accept
button.click()
driver.switch_to.alert.accept()
See also:
Handle Popup Windows
Click the javascript popup through webdriver
Unfortunately, Mechanize's browser seems to skip the pop-ups so the title, URL, and HTML are identical for both pop-ups and normal pages.
Frankly, Python is not the right tool for this job and is lagging behind in this respect IMHO. Having spent months doing web crawling, for sites that use Javascript extensively (the number of which is greatly increasing nowadays), I find that using Javascript-Based environments like PhantomJS or SlimerJS are simply better for what you're trying to do.
If you have the luxury to use Javascript-Based environments, I'd say go right ahead. However, you can still use python. PhantomJS embeds Ghost Driver. You can use Ghost.py to utilize the power of PhantomJS. Or you can use Selenium with Python as illustrated here.

Closing a generic pop up in Selenium

While scraping a page using selenium webdriver, there is a "pop up" that appears .
On Opening the page, http://www.fanatics.com/search/red%20shoes - I see a popup window with xpath '//*[#id="mm_DesktopInterstitialImage"]' - but I don't want to be using the xpath to close this alert, and have something genric that can dismiss/close the alert.
Here's what I tried so far -:
from selenium import webdriver
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
chromedriver = "/usr/local/CHROMEDRIVER"
desired_capabilities=DesiredCapabilities.CHROME
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver,desired_capabilities=desired_capabilities)
url='http://www.fanatics.com/search/red%20shoes'
driver.get(url)
#driver.set_page_load_timeout(300)
driver.implicitly_wait(60)
alert = driver.switch_to_alert()
alert.dismiss
handle=driver.window_handles
print handle
#[u'CDwindow-B1E9C127-719D-ACAA-19DE-1A6FA265A4FF']
From what I understand from related examples, folks usually switch window handles, but my handle has a single element.i.e, driver.switch_to_window(driver.window_handles[1]) then driver.close() and finally shift again, using driver.switch_to_window(driver.window_handles[1]) I also used implicit wait, since I was not sure, if the alert window was being read at-all - but that did not work either. I do not wnat to hard-code for the xpath,if that is possible.
What am I doing wrong ?
Related, but does't work for me : Selenium python how to close a pop up window?
As I can see, it's not an alert box!!. It is just a Simple Pop-up that appears when you are entering the page and it is present in main window itself(no need of switching and closing it too). Use the below code to close it.
driver.find_element_by_xpath('//div[contains(#class,"ui-dialog") and #aria-describedby="dialogContent2"]//button[#title="Close"]').click()
It works perfectly, give it a try ;)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--disable-notifications")
browser = webdriver.Chrome('C:\Python34\Lib\site-packages\selenium\webdriver\chromedriver.exe', chrome_options=options)
The popup you are trying to close is not a browser alert but a DOM popup. A browser alert is a dialog box that the browser creates at the OS level. (It is appears as an additional window on your desktop.) You can use .switch_to_alert() to close these alerts. A DOM popup is just an HTML element that happens to be used the the page as if it were a dialog box. This popup has existence only in your browser. (The OS does not know about it.) You cannot use .switch_to_alert() to close DOM popups.
To get this popup and close it, you have to inspect the DOM (like you've started doing), find the DOM element that represents the close button and have Selenium click it.
I ran into a similar problem, except that our pop-up's closebox webelement had a dynamic id tag, making it difficult to latch onto. We ended up solving it by using By.className to find it. In the code below, I use a List because I was also looking to see how many elements I was dealing with, but that's not required. Also, the production code has additional handling in case window closebox elements were found.
List<WebElement> closeboxes = driver.findElements(By.className("v-window-closebox"));
for (WebElement we : closeboxes) {
we.click();
}
You can try the following:
from selenium.webdriver import ActionChains, Chrome
driver = Chrome('/usr/local/CHROMEDRIVER')
ActionChains(driver).move_to_element_with_offset(
driver.find_element_by_xpath('//html'), 0, 2338
).click().perform()
This will click an area outside of the popup thus closing it. 🤠

How to check for webpages' popups?

Is there a possibility if I code a program in python that allows to automatically browse a given website using mechanize to detect if there are popup windows (suggesting advertisements or downloading actions ...) using Python ?. I would appreciate any hint (for example, if you give me a library that fulfills this task I would be very happy)
Mechanize cannot handle javascript and popup windows:
How do I use Mechanize to process JavaScript?
Mechanize and Javascript
To accomplish the goal, you need to utilize a real browser, headless or not. This is where selenium would help. It has a built-in support for popup dialogs:
Selenium WebDriver has built-in support for handling popup dialog
boxes. After you’ve triggerd and action that would open a popup, you
can access the alert with the following:
alert = driver.switch_to_alert()
Example (using this jsfiddle):
from selenium import webdriver
url = "http://fiddle.jshell.net/ebkXh/show/"
driver = webdriver.Firefox()
driver.get(url)
button = driver.find_element_by_xpath('//button[#type="submit"]')
# dismiss
button.click()
driver.switch_to.alert.dismiss()
# accept
button.click()
driver.switch_to.alert.accept()
See also:
Handle Popup Windows
Click the javascript popup through webdriver
Unfortunately, Mechanize's browser seems to skip the pop-ups so the title, URL, and HTML are identical for both pop-ups and normal pages.
Frankly, Python is not the right tool for this job and is lagging behind in this respect IMHO. Having spent months doing web crawling, for sites that use Javascript extensively (the number of which is greatly increasing nowadays), I find that using Javascript-Based environments like PhantomJS or SlimerJS are simply better for what you're trying to do.
If you have the luxury to use Javascript-Based environments, I'd say go right ahead. However, you can still use python. PhantomJS embeds Ghost Driver. You can use Ghost.py to utilize the power of PhantomJS. Or you can use Selenium with Python as illustrated here.

Categories