How can I click the close button in Gmail with Python + Selenium? - python

I want to click on the button to close the message that is being written.
I try with this code, but it does not work for me:
driver.find_element_by_xpath('//input[#type="Cerrar"]').click()
Can you help me? Thanks very much.

You need to click this element: //img[#alt="Close"]
So, the Selenium python command to do this is
driver.find_element_by_xpath('//img[#alt="Close"]').click()

unless your code needs to use selenium i think that pyautogui would be better for this. it can move your mouse to a specific coordinate or look for the close button and then click it
import pyautogui
pyautogui.moveTo(x=(your x), y=(your y))
pyautogui.click()
i dont know much about selenium so im sorry i cant use that to help you but i hope this helps.

The built-in method close:
The close() method is used to close the current browser window on which the focus is set, on the other hand, the quit() method essentially calls the driver. dispose of a method that successively closes all the browser windows and ends the WebDriver session graciously.
You want to use click when interacting with elements
driver.find_element_by_xpath('//input[#type="Cerrar"]').click()
If that doesn't work, please share the HTML code.

Related

How do I use python to auto-answer a pop-up prompt in Windows?

I use a ticketing program at work all day every day. Every 5 minutes it produces a little pop-up window asking "are you still using the program?" and if you don't click yes in 60 seconds it logs you out and closes the program. Very annoying.
I want to use Python (or whatever else works, I don't really care about the language) to make a script that detects when this window pops up and automatically presses the "yes I am still here" button on that window.
I believe that I can locate the pop-up window using Python's subprocess library and win32gui library. That is still a work in progress but if you think you have a better method I'm open to suggestions.
Once I do identify the pop-up window, what would be the best way to "click" the "yes I am still here" button? It would be nice if I could do this in the background milliseconds after the window is generated so that it's almost like the window never existed at all, but it's fine if I need to bring the window to the foreground and use something like x,y coordinates to simulate a click.
Thank you for any help!
as #barmar said, https://pyautogui.readthedocs.io/en/latest/ will help you.
You can take a screen shot and scale it to get the image file of the popup. Then, you can use some python code to locate the image and press the button.
ButtonLocation = pyautogui.locateOnScreen('bttnScshot.png')
ButtonPoint = pyautogui.center(ButtonLocation)
button7x, button7y = ButtonPoint
pyautogui.click(button7x, button7y)
Thank you for your time, I am new to
stackoverflow. :)

Struggle clicking a a window closing button with selenium

I'm struggling to click on a specific object with selenium. HTML CODE
What I've tried so far, unsuccessfully:
webD.find_element_by_link_text('Apply').click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div/div[4]/div/div[3]/button'))).click()
webD.find_element_by_xpath('/html/body/div[3]/div/div/div[4]/div/div[3]/button').click()
WebDriverWait(webD, 20).until(EC.presence_of_element_located((By.LINK_TEXT, 'Apply'))).click()
And myriads in between. The final code is the one I was most optimistic about.... Turns out the button changes from "close" to "apply" once a selection is done, so I figured by waiting for the element to appear and then click it could work. I think I'm missing something here. Maybe I don't require a wait at all..
Clicking the apply button closes the selection window (which is what I'm trying to do).
Any insight on why these are wrong? Any idea how to correct this issue? Thank you so much in advance! Looking forward to reading your answers.
Use following xpath to click on the button.
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Apply')]"))).click()

Python - Selenium can't handle alert

I'm trying to automate a procedure on a really old IE-only webpage and, at one point, it raises an alert.
I can't inspect it using IE (or don't know how) but there's only an "accept" button (actually an image) and the alert text can't be copied (not sure why).
I'm using selenium with the IE drivers and i can't get past this alert.
Selenium IS detecting the alert, but when i check its contents i get nothing.
I've tried accepting the alert with
alert_obj = self.br.switch_to.alert
alert_obj.accept()
and also
.dismiss();
.send_key(Keys.ENTER)
and some other things. Am i missing something?
photo of alert:
alerts are generic dialog boxes that include a text and ok button. If your alert has an image as button, then it is not an alert, but something else. I don't know what it is, but not an alert.
This is an alert:
If this popup is an alert, you can handle it while initializing driver itself. Below is C# Code for that:
InternetExplorerOptions options = new InternetExplorerOptions
{
UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,
};
driver = new InternetExplorerDriver(options);
Above code should handle alert. if not, try adding this code before creating driver instance.
options.AddAdditionalCapability("browserstack.ie.enablePopups", "accept");
This question is a possible duplicate of:
How to close yet another chrome popup using python selenium
To moderators: I tried to flag this question as Duplicate, but then, when I typed "How to close yet another chrome popup using python selenium" into the search bar it found no results.
I somehow managed to do it.
I tried everything and everything was buggy. I even had to use this weird loop because just using a time.sleep() call was bugging things up. I really don't know what happened. Sometimes when entering the page the pop-up handle would appear and sometimes not. Sometimes in the wrong place. Sometimes it wouldn't close properly. I tried a pile of different ways and this one seems to work:
Here's what i did:
#Saves ID from original window
janelaOriginal = self.br.current_window_handle
#Go to the website
self.br.get(url)
#waits 2.5 seconds for the pop-up (time.sleep bugs)
i = 0
while(i < 25):
i += 1
time.sleep(0.1)
#is pop-up open?:
if(len(self.br.window_handles)>1):
#handle sometimes appears in the wrong place so this is necessary:
if(janelaOriginal==self.br.window_handles[0]):
self.br.switch_to_window(self.br.window_handles[1])
else:
self.br.switch_to_window(self.br.window_handles[0])
#close the pop-up and go back to the original window
self.br.close()
self.br.switch_to_window(janelaOriginal)
#do stuff
return
#do other stuff

When i minimize chrome window, selenium stops [duplicate]

I have a project to get some information from website.
I want to look at the process inside the chrome window, So I can't use headless browser. But sometimes I want to minimize the chrome window.
But I found the selenium would go wrong after I minimize the chrome window manually, but sometimes not. When go wrong, exception
element is not clickable at the point, other element will receive the click
will be raised, or sometimes selenium just stop.
I have searched for a long time that some people said that the chrome window should be focused on and can't be minimized by clicking '-' on the window title bar. And the alternative solution is:
web.set_window_position(-2000,-2000)
To make the window move out the screen.
And someone says by simulating shortcuts to minimize the window. But I think it's the same as click '-' manually, am I wrong?
My question is :
Does selenium really requires chrome window not minimized? Why sometimes it can run selenium normally after minimized but sometimes not?
If I use
set_window_position(-2000,-2000)
to move the window invisible, then I click the window icon on the bottom of os (I'm sorry that I don't know what it's called). Normally when clicked, the window will be minimized. So as for this chrome window, Will it be considered as minimized window and go wrong?
I am really sorry for my poor English. I hope I have a clear description of my problem.
Environment:
python 3.6
chrome 66.0
selenium 3.11.0
windows server 2012
Edit to add code:
wait.WebDriverWait(driver,100000).until(EC.visibility_of_element_located((By.ID,'commMgrCompositionMessage')))
textArea = driver.find_element_by_id('commMgrCompositionMessage')
driver.execute_script("arguments[0].value="+"'"+modelStr+"';",textArea)
time.sleep(1)
wait.WebDriverWait(driver,10000).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'#sendemail_label')))
allSendMailLabel = driver.find_elements_by_css_selector('#sendemail')
allSendMailLabel = allSendMailLabel[1]
driver.execute_script("arguments[0].click();", allSendMailLabel)
If you see the question here Debugging "Element is not clickable at point" error, there is a bug in the chromedriver that causes this. The issue for it was created here. There is a workaround listed in the 27th comment, but what you can do is switch to the firefox driver and see if that works. Minimised windows should not cause a problem otherwise.

A Python screen capture script that can be called programmatically?

I want to open a page and grab the screen and obtain its mean color with PIL. I want to open about 100 pages so I thought a screen capture script with Python would be useful. I found several of them and I decided to use this simple one:
"""
A simple screen grabbing utility
#author Fabio Varesano -
#date 2009-03-17
"""
from PIL import ImageGrab
import time
time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")
But I need to run this from Command Prompt which then shows on the screen. How do I make this into a script so that I can call it programmatically? Or, what do you think is the best way to achieve this? Thanks!
You can use Selenium and any browser:
Take a screenshot with Selenium WebDriver
This is cross-platform, works on every browser.
Selenium is designed for functional web browser testing, but is suitable also for use cases like yours. More info about Python + Selenium:
http://pypi.python.org/pypi/selenium
The benefit of Selenium is that you have fine-tuned control of the browser - you can click buttons, execute Javascript, whatever you wish and the browser is no longer a black box.
This solution seems to do exactly what you want by bringing the browser to the foreground before performing the screen grab. Just replace 'firefox' with 'chrome'

Categories