SendKeys does not work when computer is locked using Selenium - python

I use python 3.9.5 and selenium 4.1.0 with Microsoft Edge 98.0.1108.43 on Windows 10 to fill a date in a text box with the code below:
#element is the web element I want to fill
element = "element_name"
date = driver.find_element(By.ID, element)
date.click()
date.clear()
#Positionate cursor at text box beginning
for i in range(1,10):
date.send_keys(Keys.ARROW_LEFT)
#Fill the date
date.send_keys("11022022")
I use a Windows task scheduler to run a python script when the computer is locked.
This code uses to work when I am logged in and using the computer, but when it's locked the send keys fail, it used to work with edge version 95, but in recent versions stopped.
Unfortunately, I can't simply replace the value of the element, apparently, the site has some different mechanism to fill it and I must fill it with the keyboard.
<input name="element_name" type="text" value="04/02/2022" id="ctl00_ContentPlaceHolder1_dtInicial_txtData" class="edt" style="width:90px;">
I also have tried to force windows to always focus on browser windows, so the webpage always has the keyboard focus, but it also does not work.
Any hint of how to solve this question? Is it a Bug?

In Selenium there is a state that called headless, it allows you to work with the web page without actually "seeing" it. There is no window opening and everything works virtually. You set this state in the browser options.
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.UseChromium = true;
edgeOptions.BinaryLocation =
#"C:\Program Files
(x86)\Microsoft\Edge\Application\msedge.exe";
edgeOptions.AddArgument("headless");
edgeOptions.AddArgument("disable-gpu");
var msedgedriverDir = #"E:\webdriver";
var driver =
new EdgeDriver(msedgedriverDir, edgeOptions);
In Java, there is a class called Robot which mimic keyboard pressing, so try to find the matching library in Python.

Related

Get the tab the user is viewing in selenium

Is there any way to determine which tab the user is viewing, like say the active tab (in selenium) is the last one and same for the user, then the user goes back to the first one, how do I know this with selenium?
From the information that has been gathered there seem to be no way to do this. What you can do is see if the current window is the active one using:
driver.execute_script("return document.visibilityState") === "visible"
But you cannot loop through the tabs and check this property since as soon as you change tab the tab you change to will also become the visible one.
While switching tabs you need to change Selenium's focus from one tab to other.
You can find a relevant detailed discussions in:
Open web in new tab Selenium + Python
How to switch window handles using Selenium and PythonHow to switch into Window using Index in
Selenium
Selenium may not have the focus on the tab you are currently visualizing. It's the user's discretion on which tab you want Selenium to focus.
You can always extract the current window handle on which Selenium have the focus using the current_window_handle attribute as follows:
print(driver.current_window_handle)

How to dynamically update Selenium methods without restarting the webdriver?

I have a Selenium bot that interacts with EA Sports FIFA 21 web app, where you can buy and sells in-game football players. The interface looks like this.
Lets say my original program was a single script that:
login() - logged into the platform
goToTransferMarket() - navigated to the ‘market search’ page
inputSearchParameters(playername) - inputted my search parameters to search
clickSearch() - clicked Search button
evaluateResults(buy_price) - evaluated results and bought players cheaper than buy price
If any method failed, I’d have to quit and restart from scratch. To fix this, I built a tkinter GUI with buttons for each function that executes in their own thread. This way If a function fails, like clicking the search button, I can manually perform the action and proceed to test the subsequent methods.
But still, to fix the broken methods, I have to restart the program from scratch which requires a tedious login process to fix something as simple as forgetting to cast a string to an integer. Logging in a lot also draws unwanted attention.
Given that I’m already logged in during the current testing session, is there a way to recompile my program and ‘grab’ the existing webdriver session?
Even better, is it possible to dynamically change my functions and update them with an ‘update with latest code’ button in my GUI?
The 'player list' box in my GUI writes to a textfile, which allows me to change who the bot is searching for in real time. The textfile is passed to the main bot search method when I click the "Bid using list" button. This is what made me wonder if I can set it up to somehow recompile parts of my code in a separate thread and keep my existing webdriver session. Any help would be amazing thank you.
You can add a infinite while loop and try catch to make sure your test never fails else :
If you are using chrome :
you can connect to existing chrome session using debuuger address, so start the chrome with specified debugging port from cmd
Start chrome with debug port: (In windows , search for other os how to start chrome in a specific debug port)
<path>\chrome.exe" --remote-debugging-port=1559
And in selenium use :
JAVA:
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "127.0.0.1:1559");
WebDriver browser=new ChromeDriver(options);`
Python :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1559")
driver = webdriver.Chrome(options=chrome_options)

One session per tab with Selenium Web-driver?

I would like to create a new session for each tab I open and then control the sessions individually using Selenium python. Is this possible?
#reynoldsnp: Firefox has an official addon that does this, but I'm not sure if you can get selenium to interact with the addon. addons.mozilla.org/en-GB/firefox/addon/multi-account-containers If you figure out a way to do it, I would love to know how.
(I can't comment yet due to my reputation score, therefore quoted comment).
I don't know how to actually interact with the extension but if you have a known set of sites you would like to open:
Try this:
Make a firefox profile for your use with selenium. Multiple profiles
Windows 8/8.1/10:
Press Win + R on your keyboard.
Type firefox --new-instance --ProfileManager
Open Firefox in that profile by selecting the new profile in the setup wizard. Install the extension in that profile.
Set up the containers you would like in the extension, to, by default, open up with a specific site.
Ensure that the checkbox is ticked.
Start selenium with that profile like this:
from selenium import webdriver
profile = webdriver.FirefoxProfile('path/to/your/profile') # on windows found here: %APPDATA%/Mozilla/Firefox/Profiles
driver = webdriver.Firefox(firefox_profile=profile)
Navigate between tabs, effectively containers, using selenuium.
First, no, you cannot. While tabs runs as a process, they are attached to the session ID which initially open the browser. This is how the protocol works https://www.w3.org/TR/webdriver/#new-session
They have, however, a unique ID which you can use to identify them by and switch between them.
driver.window_handles
will give you the list of open tabs. Each tab is fully isolated. You can now choose between
driver.switch_to_window("any open tab taken from windows handles list")
driver.do_something
driver.switch_to_window("any other tab from windows handles list")
driver.do_something_else_on_other_tab
# or (this option can let you run in parallel)
driver a = ChromeDriver()
driver b = ChromeDriver()
a.do_something
b.do_something
As suggested (and I personally do myself) open new session for each tab you want, that way you can parallel them and run much faster, all in all.
I am not sure the performance difference is that significant between multiple browsers or multiple tabs... they should use almost the same resources.

how to switch to windows security window and insert credentials

Here are my steps
1.Using selenium webdriver I am opening Edge
2.Then after click a link it opens popup security window. I am attaching a screenshot
3. I tried to switch that window using many different ways. It is not an alert because when I called for alert it says no such alerts are open.
I tried to call following but it gives error
window_after = driver.window_handles[1]
Following two lines gave me 1 handles:
handles = driver.window_handles
print("Number of handles ", len(handles))
Output:
Number of handles 1
if it is not alert, not a window, what is it? view source code does not print anything.
All i need is insert user name and password and then click "OK" button
I put the program for sleep 20 seconds and then manually selected the cursor to user name text box. The executed following code but it does nothing
ActionChains(driver).send_keys("Test").perform()
Side Note: Need answer for only for Microsoft Edge.
Selenium only works for browser automation. The Windows Security pop-up is an OS-level dialog and Selenium will not be able to recognize it. You could use third party tools like AutoIt with Selenium to automate non-browser based functionality. For the detailed steps, you could refer to this article.

WebDriver does not return to default content

I'm trying to write a test for a pair of sites taking part in the online support system: one is for Agent, the second is for the Visitor.
Initially, the Visitor opens the support tab on his site and the Agent gets the call on his side and picks it up. Both sides get the text chat only at this moment.
After that, the Agent wants to escalate the chat-call to Audio-Video level and presses a special button for that.
An additional widget opens containing the IFRAME element with INVITE button.
Agent presses the INVITE button.
After that the Agent has to press the Send button in the textual chat (that is on the main page content, not in the IFRAME) to send the invitation for the agent to join the Audio-Video chat.
These are the steps that I want to automate.
Everything is working until the .switch_to_default_content(). After that, the browser seems to lose the DOM and does not find neither elements on the main page nor inside the IFRAME.
Behavior is the same for local WebDriver and for WebDriver.Remote grid, for Chrome latest and Firefox latest, on Windows 10 and on Mac OS X 10.11.6 regardless of Capabilities combination.
Below is the code in Pytnon:
#Starting the Agent browser
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
agent_browser = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=options.to_capabilities())
<...>
#Starting the Visitor browser
visitor_browser = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME)
<...>
# Finding the IFRAME
agent_LPVE_iframe = WebDriverWait(agent_browser, 30).until(EC.presence_of_element_located((By.XPATH, "//iframe[contains(#class, 'lpview_table_items_placeholder')]")))
# Switching to IFRAME
agent_browser.switch_to_frame(agent_LPVE_iframe)
<...>
# Switching to default contents
#agent_browser.switch_to_default_content()
agent_browser.switch_to.default_content()
After this, the WebDriver becomes unable to find any element in both main page DOM and inside the IFRAME even if it gets switched back to IFRAME.
Why it happens and how do I make it work correctly?
I am using:
- Python 3.6.0 x32
- selenium-server-standalone-3.3.1.jar
- ChromeDriver 2.28 Win32
- GeckoDriver 0.14.0 x64
Tell me if you need any additional information.

Categories