Python webbrowser: simple way of checking when the launch is finished? - python

I am launching a url with
webbrowser.open('url')
The problem is that sometimes it takes 5 seconds for the browser to complete its launch and other times 1 second.
I need to know when the browser if done launching so I can start clicking the website with win32api mouse clicks.

Use selenium. It has functions to find elements by x path, css selector. It can also click as instructed. The main benefit of using it: it waits till the page loads.
Clicking a button automatically in a web browser with python

Related

Timeout to a function Python on Windows

I use Selenium with Chrome WebDriver on mobile mode, I got an element and I need to click on it.
When I click, A pop-up is visible, which is good for me, but the click function is still going.
It's stuck and doesn't finish.
Also, when I click the element, there is no new screen on the browser, just an object visible, this is why the click button is not getting any response and doesn't finish.
I want to wrap this click function with a timeout to finish after a couple of seconds.
This must run on a Windows and I can not open a new process/thread for it, just finish the call function and continue with the code.
This is an idea of the code:
from selenium import webdriver
driver= webdriver.Chrome()
driver.get('https://webstie.com')
toggle_to_mobile() # THIS IS PRIVATE METHOD
element = driver.find_element_by_name("my_element")
element.click() # THIS IS STUCK
.
.
Thanks!

How to stop selenium from switching to the window it's running on when it refreshes or does something on a webpage?

I want selenium to do everything in the background and play a sound when it reaches a specific page. But every time it refreshes or changes pages, it forcefully switches my current window to the browser its running in. How can I disable it? And if I can't, what's a good alternative to get what I'm trying to do.

Selenium ActionChains key commands enacting key shortcut?

I have a Python program that uses APScheduler and Selenium to automate webscraping. Basically it scrapes a particular website once every hour, and then schedules certain more detailed scrapes to happen at intervals.
The issue is that while I want to start the scraper, and then be free to use my computer for other work, Selenium will then automatically focus on the opened chrome tabs whenever a new scrape is started by APScheduler. Because of this, I am trying to find a way to have the new chrome windows open - but I don't have to focus on them. I've already tried headless and phantomJS, but the site is dynamically generated so these don't really work.
My current solution is to open the new window, minimise it, and then immediately shift back to the old window. To do this I want to perform a keyboard shortcut using ActionChains. I currently have this test code:
driver = webdriver.Chrome(ChromeDriverManager().install())
ActionChains(driver).key_down(Keys.CONTROL)
ActionChains(driver).send_keys(Keys.RIGHT)
ActionChains(driver).key_up(Keys.CONTROL)
Currently this code does nothing however. Is there anyway to fix this? I am using Jupyter Notebook and I am on a mac for reference.
NOTE: this code is being run apart from the main program, I am just using it to test if the script will work.
Try this out. You never .perform() it.
ActionChains(driver).key_down(Keys.CONTROL).send_keys(Keys.RIGHT).key_up(Keys.CONTROL).perform()

how to work with code when web driver is open

ok; I wrote a code and use python selenium and then with pyqt5 made it graphical.
but when I push the button start webdriver opened and I can't access to my windows's code any more, It got frozen , but I should input some valuable or click on some button as the code running.
what should I do ?
I've faced the same problem before. My solution was to use the time.sleep(duration) when the web driver opened the Window. For your specific situation, I think that you can use javascript on the page to check the user input is complete or not. Once the input fields are filled then trigger the process behind.
What I did was using a Process, so it would seperate the GUI-Loop and everything what is being done by selenium in a Process. By doing so you'll be able to use your GUI parallel to running Selenium in the Background by your process.

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