I have to run a daily web search with selenium, so I made a Selenium function and put it in a infinite while loop using the time module:
while True:
selenium_function()
time.sleep(86400)
Is this the right way to do it? Or should I use instead an inner Selenium option ?
EDIT
I found (while looking for cron on YouTube...) this Python module scheduler, so my code is now like that:
schedule.every().day.at("10:30").do(selenium_function)
while True:
schedule.run_pending()
time.sleep(1)
Do you have any experience with this way?
Related
I have a python script which uses selenium and chromedriver to get some info from a website. I have used python scheduler module to make the script run every 2 minute. It runs fine and get the info I want. But, whenever another script scheduled in windows task scheduler gets triggered, this script automatically stops although there is a infinity loop in it. I don't want another script causing this script to stop. How to handle that?
This is how my code is structured.
def get_info():
# code here
schedule.every(2).minutes.until("18:30").do(get_info)
while True:
schedule.run_pending()
time.sleep(1)
I want this infinity loop to run for a long time and not interrupted by another task and script scheduled in the windows task scheduler. What should I do? Any suggestion is appreciated.
So i've done this program that helps me to manage windows and applications that i wanna have opened after i log in , and i realized that if i put this script to startup folder it'll start the program before i even login, and that's not what i want because the program depends on the time between starting application and pressing keyboard shortcut.I need the program to start after i login.I'm using pycharm with python 3.8 . This is the code i wanna run after i Login.
import os
import time
import pyautogui
os.startfile("C:\\Program Files\\JetBrains\\PyCharm Community Edition
2019.3.2\\bin\\pycharm64.exe")
time.sleep(2)
os.startfile('C:\\Users\\Igor\\AppData\\Local\\Programs\\Opera\\launcher.exe')
time.sleep(25)
pyautogui.keyDown('ctrl')
time.sleep(0.2)
pyautogui.keyDown('win')
time.sleep(0.2)
pyautogui.keyDown('down')
time.sleep(0.5)
pyautogui.keyUp('ctrl')
time.sleep(0.1)
pyautogui.keyUp('win')
time.sleep(0.1)
pyautogui.keyUp('down')
pyautogui.press('enter')
I've already looked up the same question on stack-overflow but there was no exact answer.
Take a look at this SuperUser answer. Maybe Task Scheduler has what you're looking for?
You can adjust the trigger so that it's not every time you unlock your machine but instead only when you log on.
I am trying to build a service where users can insert their Javascript code and it gets executed on a website of their choice. I use webdriver from python's selenium lib and chromedriver. The problem is that the python script gets stuck if user submits Javascript code with infinite loop.
The python script needs to process many tasks like: go to a website and execute some Javascript code. So, I can't afford to let it get stuck. Infinite loop in Javascript is known to cause a browser to freeze. But isn't there some way to set a timeout for webdriver's execute_script method? I would like to get back to python after a timeout and continue to run code after the execute_script command. Is this possible?
from selenium import webdriver
chromedriver = "C:\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
driver.get("http://www.bulletproofpasswords.org/") # Or any other website
driver.execute_script("while (1); // Javascript infinite loop causing freeze")
You could set a timeout for your driver.execute_script("while (1);") call.
I have found another post that could solve this issue.
Basically, if you are on a Unix system, you could use signal to set a timeout for your driver.execute_script("while (1); call.
Or if you could run it in a separate process and then end the process if it takes too long using multiprocessing.Process. I'm including the example that was given in the other post:
import multiprocessing
import time
# bar
def bar():
for i in range(100):
print "Tick"
time.sleep(1)
if __name__ == '__main__':
# Start bar as a process
p = multiprocessing.Process(target=bar)
p.start()
# Wait for 10 seconds or until process finishes
p.join(10)
# If thread is still active
if p.is_alive():
print "running... let's kill it..."
# Terminate
p.terminate()
p.join()
I run selenium in python to do web testing, and I have noticed that, when I have added a longer wait in python, my selenium session is logged-out after a certain point of time.
Below Line of code I use to wait in the code
time.sleep(420)
I have tried to do some fake click during the wait period, but still, I have seen the security logout. is there any approach I can solve this issue?
I use python 3.5 and firefox web driver for testing.
What if you try to insert also a wait to Selenium.
In Java it looks like: SeleniumUtils.sleepQuietly(420);
There should also be a method for Python how to tell Selenium to wait (sleep) for certain time.
In your case, you have to say Selenium to wait, after you have called "the function" or click the button that needs to long (6 minutes) to execute.
when you use sleep() function you cannot do anything in that time. that is very useful between two lines of code where you need some time for processing like you are downloading file so, wait 5 seconds after 5 seconds add new parameters and download another file something like that.
you can surely do clicks without having sleep() function. between to click use sleep of 5 to 10 seconds like this your session will not expire it will run until you do driver.quit().
Updating with more context: Selenium 1 had a command called "setSpeed." This allowed the execution of each command to be slowed down by X milliseconds. The team behind Selenium 2 (Webdriver) decided to deprecate this command and now there is no way to slow down the tests to run at speeds where it's easy to visually monitor the App during execution. I've read the developers' explanation as to why they deprecated it, as well as the suggested workarounds like using implicit_waits, but that doesn't solve the issue for me (or the other people complaining about the deprecation). That said, I was hoping to work around this by setting a global execution speed that would be applicable to either each method in unittest, or the entire suite of tests.
Original Question: I have different unit tests that I'd like to execute using different delays between commands. I know that I can keep copying and pasting time.sleep between the commands, but surely there is a way to just set a universal sleep that will be run before each command in the specified method?
def test_x_test(self):
driver = self.driver
time.sleep(2)
print("running the First selenium command such as click button")
time.sleep(2)
print("running another Selenium command such as click link ")
time.sleep(2)
self.driver.quit()
if __name__ == '__main__':
unittest.main()
Ahh now the answer is so obvious.
Create a helper method that controls webdriver actions and before it executes the action put in a pause:
The below is going to be pseudocode-ish as I no longer have access to a Python IDE at work
#passing in Webdriver instance and the command we want to execute into our helper method
webdriverHelper(driver, command):
#this 2 second sleep will get run each time
time.sleep(2)
if command == "click":
driver.getElement.click()
elif command== "getText":
driver.getElement.getText()
etc...............