0

I'm encountering a perplexing issue while using Python Selenium for web automation. The script runs smoothly when I execute it manually, but when I schedule it using the task scheduler, it consistently is failing to locate specific elements on the webpage. Here are the details of my setup:

Chrome Driver Version: 115.0.5790.170 Chrome Browser Version: 115.0.5790.171 Python Version: 3.9.17 Selenium Version: 4.11.2 // Also tried 4.9.1 Operating System: Windows Server 2019, Version 1809 Execution Mode: Task Scheduler Chrome Launch Mode: Debug mode with a specific user data directory WebDriverWait: Adequate wait times have been implemented

The script establishes a connection to the Chrome instance successfully, and the Chrome tab remains open and active throughout the process. Despite implementing WebDriverWait with sufficient wait times, the script often fails to locate certain elements on the webpage during scheduled execution.

Some key points:

  • The issue only arises when the script is scheduled through the task scheduler; manual execution works fine.
  • We have ensured that the Chrome Driver version matches the Chrome Browser version.
  • The Chrome tab remains active and visible during execution.
  • Elements are located using WebDriverWait with appropriate timeout intervals.
  • My teammates and I are at a loss trying to pinpoint the root cause of this problem. We suspect that there might be some underlying differences between manual execution and execution through the task scheduler that are contributing to this behavior.

Has anyone encountered a similar issue or have insights into what might be causing this discrepancy? Any suggestions, guidance, or troubleshooting steps would be greatly appreciated. We're eager to resolve this issue and ensure consistent automation even when using the task scheduler.

1 Answers1

0

Since the issue is manifesting when running the script using task scheduler, it might be possible that the issue is related do directory where the app is running.

I would suggest you check or do the following:

  • check the "Start In" configuration of the scheduled task (location where the script will run)
  • check your codes for use of relative directories (if you run manually it may be using the project directory as the base directory but using the scheduled task it may use a base directory in a different location)
  • add debug logs to check where the task is running
lorvindc
  • 69
  • 1
  • 7
  • But the script starts without any issue. I have my file which I want to start like python folder/file.py and I have already mentioned the same in the start in field. – aniketvp24 Aug 21 '23 at 04:41
  • Maybe you can also check ifcthere are remaining chromedriver/chrome tasks running in the background. Is the error consistent? Are there specific elements not being found? – lorvindc Aug 21 '23 at 05:26
  • No, the elements are not being found randomly, I have given appropriate wait time also, like 30s. But the thing to note is that when I am active on the server, and run from the task scheduler it runs perfectly all of the time. But when the scripts runs automatically when triggered. I have checked the Run whether the user is logged in or not option also. – aniketvp24 Aug 21 '23 at 06:08
  • Another possible idea is to have the selenium tests run in headless mode. Or maybe you can create a batch file that would trigger the python script. – lorvindc Aug 21 '23 at 07:59
  • I am already having the python file run through the batch file. I want to connect to an already open chrome tab, so headless is not possible. – aniketvp24 Aug 21 '23 at 08:02
  • turns out I am facing the same issue when running from another python script using schedule library. And this happens when I disconnect from the server. when running from the server manually, it runs without any issue whatsoever. – aniketvp24 Aug 21 '23 at 10:58
  • maybe you can also check the privileges of the actual user running the scheduled task.Maybe some of the answers in this link can be helpful. https://superuser.com/questions/1334931/task-scheduler-batch-run-whether-user-is-logged-on-or-not-not-working – lorvindc Aug 21 '23 at 13:32
  • I think I am facing this issue here --> https://stackoverflow.com/questions/73110242/why-does-chrome-selenium-throw-an-error-when-disconnected-from-the-windows-2019 – aniketvp24 Aug 21 '23 at 15:21