How to get out of a context click in python selenium? - python

This has been asked before here: How to hide context click? Selenium Chromedriver
But none of the solutions there have worked for me. There is an element on my webpage that requires a right-click to select it (weird, but it makes sense with the site). I am able to click this element using
action.context_click(src).perform()
And I have tried to get rid of the menu that pops up after right-clicking 4 separate ways
# Left-clicking in the same spot after I have right-clicked
action.click().perform()
# Hitting the space bar
webdriver.ActionChains(self.driver).send_keys(Keys.SPACE).perform()
# Hitting the escape key
webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
# Clicking on an unused element
action.move_to_element(WebDriverWait (self.driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "html"))))
action.click().perform()
# Reloading the page did not get rid of it either
self.driver.refresh()
None of these methods have worked to get rid of the menu that pops up. I cannot reload the page to get rid of this otherwise it will deselect the object I am trying to get. This does not affect my program but it is annoying to have it in my way when I am trying to spectate what my program is doing. Any help is appreciated! This is something in my free time so it is not time-sensitive.

I tried to open the context menu (right click) on a random site and then I was able to close it with pyautogui (pip install pyautogui)
action.context_click(src).perform()
import pyautogui
pyautogui.press('esc')
It may be possible that you have to press esc a couple of times.

Related

Python selenium code seems to wait after calling javascript that display modal dialog box

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()

Selenium is unable to drag and drop unless the action chain is ran twice (python)

I'm running into something strange with Selenium's action chain for drag and drop.
ActionChains(ctx.driver).click_and_hold(element_to_drag).move_by_offset(x_offset,y_offset).perform()
When I try to use it on a modal I have to run it twice for it to take effect:
ActionChains(ctx.driver).click_and_hold(element_to_drag).move_by_offset(x_offset,y_offset).perform()
ActionChains(ctx.driver).click_and_hold(element_to_drag).move_by_offset(x_offset,y_offset).perform()
I had the thought that perhaps the first action chain was somehow selecting the element and the second was dragging and dropping it so I replaced the first line with a click but that had no effect. I also tried double clicking first, clicking and pausing, and click and hold but none of those had any effect. The only way this works for me is to call it twice. Has anyone ever experienced this or does anyone have some insight? I'm running this on a mac. I've read some comments online saying there were issues with drag and drop on macOS but those are all from several years ago. Also I'm using Selenium 3.141.0 with python 3.7.0.

How to refer to active browser window and click in something there?

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

Using Winium (+ Selenium) with python only allows me to open the app, but then won't interact with it?

When I try to run any kind of code using winium, it will open the app, but then won't execute any of the code afterwards. It's not as if it throws up an error, it just hangs there and won't move on.
I Am using Python 3.7 on a Windows 10 PC.
I have tried the two 'magic' examples that are listed on the github wiki page for Winium, but even that doesn't work. I am able to use selenium to do automated web testing, so I don't think the selenium module is the issue. I have tried importing the time module and making it sleep for 10 seconds in between lines but this has no effect on the outcome.
from selenium import webdriver
driver = webdriver.Remote(
command_executor='http://localhost:9999',
desired_capabilities={
"debugConnectToRunningApp": 'false',
"app": r"C:/windows/system32/calc.exe"
})
# THIS IS WHERE IT SEEMS TO PAUSE INDEFINITELY
window = driver.find_element_by_class_name('CalcFrame')
view_menu_item = window.find_element_by_id('MenuBar').find_element_by_name('View')
view_menu_item.click()
view_menu_item.find_element_by_name('Scientific').click()
view_menu_item.click()
view_menu_item.find_element_by_name('History').click()
window.find_element_by_id('132').click()
window.find_element_by_id('93').click()
window.find_element_by_id('134').click()
window.find_element_by_id('97').click()
window.find_element_by_id('138').click()
window.find_element_by_id('121').click()
driver.close()
I would expect it to press the corresponding buttons, but it doesn't seem to do anything except open the calculator app.
I think this example is written for an older version of calculator. In Windows 10, the "Scientific" button is under the Menu button.
You'll have to find the menu button, click it, and then look for the element "Scientific" in the list.
Also, the numeric values for your arithmatic case are not correct. Pick up a UI inspector tool (inspect.exe, uispy, etc...) to make sure you are targeting the elements correctly.

How to handle javascript alert/pop up window: Python+Selenium IE webdriver

The webpage I need to go to has a javascript popup message with an OK button that appears before the page can finish loading.
There's ways to get rid of the message by either clicking the button, pressing enter, pressing the x to close, or pressing ALT+F4. But all attempts to either click or press keys fails and just remains on the page with the popup.
I must be missing something.
Everything is current and installed, even the registry additions for the IE server executable.
Thanks
If this Java popup is generated by IE you could be able to handle it using Alert(driver).accept()
I have run into this issue a couple of times where the popup is not part of IE at all and this does not work. there are a couple different methods you can try to get around this:
try switching your browser to phantomJS (this is a headless browser that works with selenium and runs in the background so you will not be able to see what selenium does anymore but it often will eliminate popups and is good if you don't actually need to click the popup)
try using the mouse or keyboard packages to hit that button.

Categories

Resources