需要在前端的数据库中使用 Python 和 Selenium 停用用户,但引用过时

Need to Deactivate Users with Python and Selenium in a Database on Front-end but Stale Reference

所以我的任务是从前端的数据库中不活动的多个用户中删除安全组。我被禁止进行后台管理。所以我通过 Selenium 和 Python 将其自动化。我很好奇我从程序中得到的以下堆栈跟踪:

  File ".\remove_sec_groups_inactive_users.py", line 127, in <module>
    element.send_keys(config['sec_group_removal']['target_user']['name_key']);
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=80.0.3987.149)

问题出在这部分附近的代码周围。

# Load users.json File
with open('users.json','r') as f:
          config = json.load(f)

# Send Keys to Lookup
# XPATH = //*[@id='brSecuredUsersLookupInput']
target_user = browser.find_element_by_id("brSecuredUsersLookupInput")
element.send_keys(config['sec_group_removal']['target_user']['name_key']);
element.send_keys(Keys.RETURN);

我希望能够让更多用户使用 JSON 有效载荷文件,但出于某些奇怪的原因,它不喜欢这样。我考虑过使用

(config['sec_group_removal']['target_users']['user']['name_key'])

来自如下所示的文件:

{
        "sec_group_removal": {
                "target_user": {
                         "user": {
                                "user_name": "firstname.lastname",
                                "name_key": "ABDLFNSDFKNE0033"
                          }
                }
        }
}

唉,这也不起作用,所以我正在使用以下内容:

{
        "sec_group_removal": {
                "target_user": {
                                "user_name": "NNNNNNN",
                                "name_key": "######"
                }
        }
}

无论哪种方式,它都没有将我需要的密钥发送到循环组件以搜索用户:

我需要帮助将字符串发送到该查找中,下面是网络元素:

<input class="lookupInput" type="text" name="brSecuredUsersLookupInput" id="brSecuredUsersLookupInput" title="Last First, Name Key, SSN, Name ID, Login" style="width: 100px;" tabindex="1" onkeydown="return lookupKeyPressed(event,&quot;&quot;,&quot;ssusrbrws001.w&quot;)" origvalue="" det="true" aria-labelledby="" autocomplete="off">

问题出现在我如何调用那里的整体变量,我试图通过 send_keys 方法。

target_user = browser.find_element_by_id("brSecuredUsersLookupInput")
element.send_keys(config['sec_group_removal']['target_user']['name_key']);
element.send_keys(Keys.RETURN);

应该是:

target_user = browser.find_element_by_id("brSecuredUsersLookupInput")
target_user.send_keys(config['sec_group_removal']['target_user']['name_key']);
target_user.send_keys(Keys.RETURN);