【问题标题】:Javascript Dialog Box Python SeleniumJavascript 对话框 Python Selenium
【发布时间】:2020-05-15 07:47:18
【问题描述】:

我正在尝试使用 Python selenium 包进行自动化,这需要登录以下页面并执行一系列任务。这样做时,webdriver 需要进入对话框并浏览对象和导入文件的地方。从 selenium 文档中,我看到 switch_to_alert() 但我知道它是用于 js 警报。为了与对话框交互,我需要使用其他包吗?非常感谢您的帮助。

[我的代码]

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

chrome_browser = webdriver.Chrome('C:/Users/vbabu/AppData/Local/chromedriver')
chrome_browser.maximize_window()
chrome_browser.get('https://idcs-b3fbc99e47864601a4d679df46d79328.identity.oraclecloud.com/ui/v1/adminconsole/?root=users')

time.sleep(5)

user_name = chrome_browser.find_element_by_id('idcs-signin-basic-signin-form-username')
user_name.send_keys('abc')
password = chrome_browser.find_element_by_id('idcs-signin-basic-signin-form-password')
password.send_keys('test')
signin = chrome_browser.find_element_by_id('idcs-signin-basic-signin-form-submit')
signin.click()

time.sleep(10)

importusers = chrome_browser.find_element_by_id('idcs-users-toolbar-import-users')
importusers.click()

time.sleep(5)

popup = chrome_browser.switch_to_alert()

#switchwin = chrome_browser.find_element_by_id('idcs-users-import-dialog_layer_surrogate')
browsebtn = chrome_browser.find_element_by_id('idcs-users-import-file-upload-button')
browsebtn.click()

[Javascript 弹出脚本]

<div id="idcs-users-import-dialog" data-bind="attr: {title: bundle('userManagement.manageUsers.userImportDialogTitle')},
      ojComponent: {component: 'ojDialog', initialVisibility: 'hide', close: closeImportUserDialog,
      rootAttributes: { style: 'min-width: 900px; width: 950px; min-height: 300px; height: 500px;'}}" class="oj-dialog oj-component oj-component-initnode oj-draggable oj-resizable oj-subtree-hidden" tabindex="-1" role="dialog" aria-labelledby="ui-id-37" style="min-width: 900px; width: 950px; min-height: 300px; height: 500px; top: auto; left: auto; bottom: auto; right: auto; display: none;" aria-hidden="true"><div class="oj-dialog-header oj-dialog-header-close ui-draggable-handle"><div class="oj-dialog-title" id="ui-id-37">Import Users</div><button class="oj-dialog-header-close-wrapper oj-button-jqui oj-button oj-component oj-enabled oj-button-half-chrome oj-button-icon-only oj-component-initnode oj-default" aria-labelledby="ui-id-243" title="Close" tabindex="1"><div class="oj-button-label"><span class="oj-button-icon oj-start oj-component-icon oj-fwk-icon-cross"></span><span class="oj-button-text oj-helper-hidden-accessible" id="ui-id-243">Close</span></div></button></div>
  <div class="oj-dialog-content oj-dialog-default-content"><div class="oj-dialog-body">
    <div class="oj-flex oj-padding" data-bind="visible: importStep() === 1">
      <div class="oj-flex-item oj-sm-12">
        <div class="oj-flex oj-flex-items-pad oj-sm-align-items-center">
          <div class="oj-flex-item oj-sm-1 idcs-align-center idcs-text-lg idcs-text-color-active">
            <span class="idcs-icon idcs-user"></span>
          </div>
          <div class="oj-flex-item oj-sm-10">
            <div class="oj-flex oj-flex-items-pad">
              <h3 data-bind="text:bundle('userManagement.manageUsers.userImportDialogHeading')">Import Users</h3>
            </div>
            <div class="oj-flex oj-flex-items-pad">
              <p data-bind="text:bundle('userManagement.manageUsers.userImportDialogInfo')">Select a file to import by using the browse button and then click Import. For additional help, please use the help links on the right side of this window.</p>
            </div>
          </div>
        </div>

        <hr>

        <div class="oj-flex oj-md-justify-content-space-between oj-margin-top">
          <div class="oj-flex-item oj-sm-9">
            <div class="oj-flex oj-md-align-items-center">
              <div class="oj-flex-item oj-sm-12 oj-md-4 idcs-text-right oj-padding-horizontal">
                <h4 data-bind="text: bundle('userManagement.manageUsers.selectFile')">Select a file to import</h4>
              </div>
              <div class="oj-flex-item oj-sm-12 oj-md-5 oj-form-control oj-choice-row-inline manage-user-input">
                <input disabled="" id="dictionaryLocation" type="text" class="oj-form-control oj-inputtext-nocomp" data-bind="value: importFileLocation, attr: {title: bundle('userManagement.manageUsers.userImportHint') }" title="You can use a file with the CSV extension to import users into Identity Cloud Service.">
              </div>
              <div class="oj-flex-item oj-flex oj-sm-12 oj-md-2 oj-md-justify-content-center">
                <span>
                  <label for="idcs-users-import-file-upload-button" class="oj-component oj-enabled oj-button-text-icon-start oj-component-initnode oj-default">
                    <input id="idcs-users-import-file-upload-button" type="file" class="idcs-hide" data-bind="value:importFileLocation, required: true" accept=".csv">
                    <button id="browse-button" class="oj-padding-start oj-button-jqui oj-button oj-component oj-enabled oj-button-full-chrome oj-button-text-only oj-component-initnode oj-default" data-bind="attr: { title: bundle('userManagement.manageUsers.userImportHint')},
                                         click: fileBrowseClickListener, ojComponent: {component: 'ojButton', label: bundle('generic.browse')}" title="You can use a file with the CSV extension to import users into Identity Cloud Service." aria-labelledby="ui-id-36"><div class="oj-button-label"><span class="oj-button-text" id="ui-id-36">Browse</span></div></button>
                  </label>
                </span>
              </div>

[弹窗截图]

[预期输出]

  • 需要进入对话框并进行一些交互。在本例中,单击浏览,选择文件并单击导入按钮。

【问题讨论】:

    标签: javascript python selenium automation


    【解决方案1】:

    您不能使用 switch_to_alert。弹出对话框是 div,而不是警报。

    JS alert 不属于 DOM 树。

    查找弹出对话框

    如下:

    如果无法与对话框交互,请检查

    是否在框架中。
    popup = chrome_browser.find_element_by_id('idcs-users-import-dialog')
    

    如果要上传文件,请使用 send_keys 到 元素。

    browsebtn = chrome_browser.find_element_by_id('idcs-users-import-file-upload-button')
    browsebtn.send_keys(//Your file path//)
    

    【讨论】:

    • 非常感谢!我会记下这个
      .
    猜你喜欢
    • 1970-01-01
    • 2010-11-28
    • 2015-01-23
    • 1970-01-01
    • 2020-08-31
    • 2021-11-09
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多