Skip to content

Keyword Input Password throws an exception when run with Python (no robotframework) #1746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mikahanninen opened this issue Sep 30, 2021 · 2 comments

Comments

@mikahanninen
Copy link
Member

mikahanninen commented Sep 30, 2021

For issues

Steps to reproduce the issue

from SeleniumLibrary import SeleniumLibrary
from time import sleep

library = SeleniumLibrary()

def minimal_task():
    try:
        library.open_browser('https://docs.robocorp.com')
        library.input_password("//input[@aria-label='Search']", "playwright")
        sleep(5)
    finally:
        library.close_all_browsers()
    print("Done.")


if __name__ == "__main__":
    minimal_task()

Error messages and additional information

Traceback (most recent call last):
  File "task.py", line 23, in <module>
    minimal_task()
  File "task.py", line 15, in minimal_task
    library.input_password("//input[@aria-label='Search']", "playwright")
  File "C:\Users\User\AppData\Local\robocorp\live\199e494e4c733ef3\lib\site-packages\SeleniumLibrary\keywords\formelement.py", line 269, in input_password
    self._input_text_into_text_field(locator, password, clear, disable_log=True)
  File "C:\Users\User\AppData\Local\robocorp\live\199e494e4c733ef3\lib\site-packages\SeleniumLibrary\keywords\formelement.py", line 509, in _input_text_into_text_field
    previous_level = BuiltIn().set_log_level("NONE")
  File "C:\Users\User\AppData\Local\robocorp\live\199e494e4c733ef3\lib\site-packages\robot\libraries\BuiltIn.py", line 3006, in set_log_level
    old = self._context.output.set_log_level(level)
  File "C:\Users\User\AppData\Local\robocorp\live\199e494e4c733ef3\lib\site-packages\robot\libraries\BuiltIn.py", line 64, in _context
    return self._get_context()
  File "C:\Users\User\AppData\Local\robocorp\live\199e494e4c733ef3\lib\site-packages\robot\libraries\BuiltIn.py", line 69, in _get_context
    raise RobotNotRunningError('Cannot access execution context')
robot.libraries.BuiltIn.RobotNotRunningError: Cannot access execution context

Expected behavior and actual behavior

Keyword Input Password works with and without Robot Framework

Environment

Browser: default by Open Browser keyword
Browser driver: Gecko
Operating System: Windows 10
Libraries

  • Robot Framework: NOT USED
  • Selenium: 3.141.0
  • SeleniumLibrary: robotframework-seleniumlibrary==5.1.3
  • Interpreter: Python 3.7.5 | packaged by conda-forge | (default, Jan 28 2021, 18:15:34) [MSC v.1916 64 bit (AMD64)] on win32
@Harri
Copy link
Contributor

Harri commented Oct 1, 2021

Would a fix like this be acceptable?

diff --git a/src/SeleniumLibrary/keywords/formelement.py b/src/SeleniumLibrary/keywords/formelement.pyindex 15ef612..ed38285 100644
--- a/src/SeleniumLibrary/keywords/formelement.py
+++ b/src/SeleniumLibrary/keywords/formelement.py
@@ -17,7 +17,7 @@
 import os
 from typing import Optional, Union

-from robot.libraries.BuiltIn import BuiltIn
+from robot.libraries.BuiltIn import BuiltIn, RobotNotRunningError
 from selenium.webdriver.remote.webelement import WebElement

 from SeleniumLibrary.base import LibraryComponent, keyword
@@ -504,11 +504,17 @@ class FormElementKeywords(LibraryComponent):
         element = self.find_element(locator)
         if clear:
             element.clear()
+
         if disable_log:
-            self.info("Temporally setting log level to: NONE")
-            previous_level = BuiltIn().set_log_level("NONE")
+            try:
+                self.info("Temporally setting log level to: NONE")
+                previous_level = BuiltIn().set_log_level("NONE")
+            except RobotNotRunningError:
+                self.info('RF log levels not available when RF is not running.')
+
         try:
             element.send_keys(text)
         finally:
-            if disable_log:
+            if disable_log and 'previous_level' in locals():
                 BuiltIn().set_log_level(previous_level)

@cmin764
Copy link

cmin764 commented Apr 7, 2023

That looks good, although I'd avoid the black-magick of 'previous_level' in locals() (and check for previous_level against None if the call of set_log_level() will return just strings).

Any intention to open a PR with the change solving this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants