0

Environment: node.js, Windows 10, cmd

Npm: Selenium-webdriver (chromedriver), ... Express.js, node-schedule, node-windows

I have seen multiple similar posts on this topic here, but none of them were able to help me. It also took me a little time to understand my own situation.

I am spawning a Selenium automated chrome browser from an Express.js server terminal. I am using the node-schedule package to schedule the start time of the automated chrome. It is fine when I have focus on the server cmd terminal - Chrome gets focus. However, when I don't have focus on the server terminal, then the automated chrome is always in background. It is not practical for me to always have the server terminal in focus 24/7.

Only cmd terminal supports the above mentioned behavior. Git bash and power shell are not helpful at all.

I have tried all the generic suggested solutions mentioned below with no success:

  • chrome.executeScript(windows.focus())
  • minimize and maximize, or resize the chrome window 4 times
  • I read the chromedriver reference and tried sending multiple optional parameters to the driver with no success

One notable advice I saw stated that Selenium has no control over switching focus between window processes. That SO solution advised on using a Java based program that helps with bringing focus to processes as the OP of that SO thread was using Selenium - Java. Unfortunately, I did not save the URL and now I cannot find that Stack Overflow thread.

I am looking for any javascript based program that will help me bring either the chrome window or the server terminal to foreground.

Thank you for your time and advices

Tenzin Thabkhae
  • 75
  • 1
  • 10
  • The system restricts which processes can set the foreground window. The rules are here https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow. Your program must comply with one of the nine rules. EU – user14797724 Jan 22 '21 at 22:46
  • *minimize and maximize, or resize the chrome window 4 times* does nothing. – user14797724 Jan 23 '21 at 22:53
  • "The foreground is not locked". Can this condition be satisfied somehow? I am not familiar with Windows 10 processes. I understand resize does nothing. Those were a "hack" suggestion that didn't work for me – Tenzin Thabkhae Jan 24 '21 at 00:51
  • It is designed to stop you doing this. Programs may not interrupt the interactive user. If you aren't allowed then the taskbar button will flash three times. – user14797724 Jan 24 '21 at 00:58
  • Yes, I see yellow flashes. So there is no solution to this? Okay. thank you – Tenzin Thabkhae Jan 24 '21 at 01:00
  • @user14797724, I am making my backend server into a Windows service (via 'node-windows'). I plan to spawn a cmd terminal from the service. Does this rule also apply here? This spawned terminal will always be in the background since the service is always in the background? My project will lose most of its value if I cannot bring the chrome window into foreground – Tenzin Thabkhae Jan 24 '21 at 01:08
  • Most windows services are invisible because they run on an invisible desktop. – user14797724 Jan 24 '21 at 01:57
  • *Services have always run in session 0. Before Windows Vista, the first user to log on was also assigned to session 0. Now, session 0 is reserved exclusively for services and other applications not associated with an interactive user.* https://learn.microsoft.com/en-us/windows/win32/services/service-changes-for-windows-vista – user14797724 Jan 24 '21 at 02:31
  • You architected you app in a way that tries to emulate user's. Programs have there own ways of doing things. What are you trying to do. – user14797724 Jan 24 '21 at 02:40
  • I am making a full-stack web app, whose backend is now a service. TLDR - my backend downloads a PDF file via Selenium-spawned automated chrome window. The backend has a map that stores "node-schedule" scheduler objects (that's an npm package). These schedulers run a script on the scheduled time (with recurrence); that script spawns the selenium chrome. The chrome window navigates to an intranet site of the company and ultimately downloads one out of 4 or 5 different PDF files depending on data saved in the database. Selenium does not have a code that downloads the file, therefore I am using – Tenzin Thabkhae Jan 24 '21 at 05:04
  • Selenium does not have a code that downloads PDF files. I am using a visual basics script via a bash script that sends the "ctrl" and "s" keystrokes. Initially, I also added delays, "tab"s, path value, and "enter" key, but I don't need to do them anymore since Selenium has a default download path value I can send to its driver. I NEEDed to bring chrome window to foreground so that "ctrl" and "s" can be entered on this chrome window. Clicking on those PDF files result in opening them in a new tab via a "Servlet" page, with the only way to download it being via "ctrl" + "s" keystrokes – Tenzin Thabkhae Jan 24 '21 at 05:11
  • See https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms759148(v=vs.85) for one way to get a page into a variable. Also see https://learn.microsoft.com/en-us/previous-versions/hh771991(v=vs.85). – user14797724 Jan 24 '21 at 05:21
  • https://stackoverflow.com/questions/27977752/download-and-execute-with-vbs – user14797724 Jan 24 '21 at 05:31

0 Answers0