Python3.x, How to focus on newly opened webpage - python

Ok, here's the deal. I use:
driver = webdriver.Firefox(firefox_profile=profile)
driver.get = ('http://google.com')
to open a new webpage. After opening the new webpage, I use pyautogui to do some clicking and scrolling and whatnot, and then close the browser, and then do the same thing over again as the program iterates through a list of proxies. But when the webpage opens, it isn't in focus. I can see it and see that it opened, but it's not in focus.
So I originally implemented a simple click command on the webpage, and that usually focuses the new browser. But after the first browser is opened and closed, the next browser that is opened using a new proxy from the list isn't in focus, and for whatever reason, the click doesn't bring it into focus. So because the browser isn't in focus, my scroll code doesn't happen, it just acts like it skips over it. So I guess my question is, is there some command I can use to make my program focus on the new browser? I've searched extensively on here for the answer to my question and have tried a bunch of different things, but none have worked. Most answers on here pertain to newly opened tabs, and that isn't my issue. Any helpful comments or ideas would be much appreciated. I'm using Mac OS X, and the most recent version of python. Thank you.

Scratch the whole click on the dock icon thing, after extensive testing, that didn't work consistently. However, what did actually work consistently was using pyautogui to click on the "new tab" button at the top, and then close tab, and boom, webpage focused.

Related

Gain colour of a known coordinate on screen in python

I'm trying to automate a specific task using python. I need to execute a specific task when a specific colour pops up on the display. For example:
During the automation process, when the code to open a web browser is executed and a web browser pops up,I need to make sure that it actually worked, by detecting the colour at the exact coordinate where the logo of the browser is situated. So that it confirms the browser is open, then I need to use that information to execute another code.
As we know, when we try to open a browser, there will be a lag to open up the browser. Since that lag will be different, I need to execute the next code only after opening the browser.
I have gone too far with my explanation, but if you got any idea from what I have just explained, please help me with what you know?

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.

python: open unfocused tab with webbrowser

I would like to open a new tab in my web browser using python's webbrowser. However, now my browser is brought to the top and I am directly moved to the opened tab. I haven't found any information about this in documentation, but maybe there is some hidden api. Can I open this tab in the possible most unobtrusive way, which means:
not bringing browser to the top if it's minimzed,
not moving me the opened tab (especially if I am at the moment working in other tab - my process is working in the background and it would be very annoying to have suddenly my work interrupted by a new tab)?
On WinXP, at least, it appears that this is not possible (from my tests with IE).
From what I can see, webbrowser is a fairly simple convenience module that creates (probably ) a subprocess-style call to the browser executable.
If you want that sort of granularity you'll have to see if your browser accepts command line arguments to that effect, or exposes that control in some other way.

Opening links in external browser in Amarok 1.4

I've tried asking this question on the KDE development forum, but haven't received a satisfying answer so far.
I've developed a Python script for Amarok 1.4 which retrieves upcoming events for the currently playing artist and displays them in the context browser. The user can click each event to know more about it, but so far clicking takes him to another Amarok tab, in which he must then click another button to finally get the link to open in the external browser.
What I'd like to know is whether there is a way to open the link directly in the external browser.
Update: I recently started using Pana, which actually opens the links directly in the Wikipedia tab, so I guess I'll stick to that player in the future.
I know nothing about Amarok, but in general you can spawn the platform's default web browser on a URL:
on modern open-source desktops (KDE, GNOME, Xfce) by spawning the xdg-open command;
on OS X with the open command;
on Windows with the built-in os.startfile method.
There is also the webbrowser module, but it will often pick the wrong browser and everything about it is outmoded and ugly.

Categories