Как обрабатывать всплывающие уведомления в Selenium с помощью Python для Chrome драйвера - PullRequest
0 голосов
/ 08 мая 2020

Итак, у меня есть код, который автоматизирует загрузку файла .csv, который мы используем для импорта данных в нашу базу данных SIS. У меня проблема с всплывающим окном предупреждения при использовании Selenium. Всплывающее окно появится только при наличии объявлений из системы.

Есть две кнопки, на которые я хотел бы нажать, и я попытался переключиться в это окно, но оно не сработало:

# Browser Switches to Window
WebDriverWait(browser,10).until(EC.number_of_windows_to_be(2))
browser.switch_to.window(browser.window_handles[-1])

# UI Container Handle for Notifications Window that Pops Up.
element = WebDriverWait(browser, 20).until(
                EC.text_to_be_present_in_element((By.ID, "whatsNewContainer"), "Submit"))
element.click();

Позже однажды я заставил его работать, Я, вероятно, добавлю туда условие if, чтобы проверить наличие всплывающих окон. Две кнопки, которые мне нужно нажать, следующие:

<input type="checkbox" id="blockWhatsnewModal" value="Y" name="blockWhatsnewModal" class="ng-pristine ng-valid ng-untouched ng-empty" ng-true-value="'Yes'" ng-false-value="'No'" ng-model="whatsNew.checkValue">
<button type="button" ng-show="updateWhatsNewFlag == 'failure'" translate="submit" class="btn btn-sm btn-success ng-scope" ng-click="updateShowWhatsNewFlag()" style="">Submit</button>

Объявление выглядит так: enter image description here

Как лучше всего обрабатывать всплывающие предупреждения в Python Selenium, используя Chrome Driver?

Вот информация о div для всплывающего окна

<div class="custom-dialog modal-content ng-scope">
   <header id="header" class="header">
      <div id="logo-group-whatsnew" class="logo-group">
         <span id="logo-whatsnew" class="logo">
            <img src="assets/images/logo1.png" alt="CollegeBoard Accuplacer">
         </span>
      </div>
      <a href="#" class="close close-btn" data-dismiss="modal">×</a>
   </header>

   <div class="row no-space">
      <div class="col-lg-12 col-md-12  col-sm-12  ">
         <div class="alert alert-success ng-binding ng-hide" ng-show="Message.length">
            <button type="button" class="close" ng-click="clearMessages()" aria-hidden="true">×</button>
            <i class="fa-fw fa fa-check"></i>  
         </div>

      </div>
   </div>

   <div>
      <div class="modal-header text-center">
         <h1 id="whatsNewModalLabel">
            <strong>What's New</strong>
         </h1>
      </div>
      <div class="modal-body">
         <article class="col-sm-12 col-md-12 col-lg-12 sortable-grid ui-sortable">
            <div data-widget-deletebutton="false" data-widget-editbutton="false" data-widget-colorbutton="false" id="wid-id-60" class="jarviswidget jarviswidget-color-darken jarviswidget-sortable" role="article">
               <header>
                  <span class="widget-icon">
                     <i class="fa fa-star"></i>
                  </span>
                  <h2>What's New</h2>
               </header>
               <!-- widget div-->
               <div id="whatsNewContainer" role="contentinfo">
                  <div class="widget-body">
                     <div id="whatsNewData"><p style="text-align:center"><span style="font-family:lucida sans unicode,lucida grande,sans-serif"><span style="font-size:18px"><strong>Last update - May 8, 2020</strong></span></span></p>

<p style="text-align:center">&nbsp;</p>

<p style="text-align:center"><span style="font-family:lucida sans unicode,lucida grande,sans-serif"><strong><span style="font-size:18px">Upcoming System Maintenance</span></strong></span></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">Starting Sunday, May 17, <strong><span style="color:#FF0000">ongoing</span></strong> standard system maintenance is scheduled for Sunday evenings starting at 10:00PM EST and ending on Monday at midnight EST (2 hours total).&nbsp;<strong><span style="color:#FF0000"> Please avoid using the system at this time. </span></strong>For&nbsp;Sunday night, <strong>May 31</strong>, the outage will be extended until <strong>2AM&nbsp;EST on June 1 (4 hours total)</strong>.</span></span><br>
&nbsp;</p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">When resuming open test sessions after this system maintenance, test-takers will be required to start the test over from the first question. To avoid this, <strong>please have test-takers complete open tests&nbsp;Sunday</strong>, <strong>before the maintenance window begins</strong>. Otherwise, the test-taker will have to start from the beginning of the test when they resume.&nbsp;&nbsp;</span></span></p>

<hr>
<p style="text-align:center"><span style="color:#FF0000"><strong><span style="font-size:18px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">An Update on Remote Proctoring with Examity</span></span></strong></span></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">We are pleased to announce that Examity live proctoring services have resumed with some availability through April, and greater availability starting May 1. Any student appointments cancelled due to their testing pause have been contacted by Examity to reschedule.&nbsp; If you are new, please visit <a href="https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fexamity.com%2Faccuplacer%2F&amp;data=02%7C01%7Csherz%40collegeboard.org%7C061ea1b66f8b4a916b0b08d7e164bf36%7C7530bdedfd6e4f58b5d2ea681eb07663%7C0%7C0%7C637225695135282433&amp;sdata=4jFn2XJwyeSUUCdeHHYpcCLwRtLRmw4tRKVYZh7UaR4%3D&amp;reserved=0" target="_blank">Examity </a>to get started.&nbsp; Please note, they have extended $20 per session through June 15.</span></span></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">Video Chat Web Service proctoring will remain an option for schools/institutions until schools/institutions are back to normal campus test center operations.</span></span></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">Additional Information regarding more options will be available shortly.&nbsp;</span></span></p>

<hr>
<p style="text-align:center"><span style="font-size:18px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif"><strong>New Remote Proctoring Solution Available</strong></span></span></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">With test center closures due to COVID-19, ACCUPLACER may now be proctored using video/chat conferencing services such as Zoom, GoToMeeting, BlueJeans, and Google Meet.&nbsp;</span></span></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">Click <a href="https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcollegeboardtraining.webex.com%2Fcollegeboardtraining%2Fldr.php%3FRCID%3Dff522bdd7e29cf2151383a00deb9579b&amp;data=02%7C01%7Csherz%40collegeboard.org%7C6c83f638000f48fac90708d7dbf85552%7C7530bdedfd6e4f58b5d2ea681eb07663%7C0%7C0%7C637219731931120715&amp;sdata=UAo6gFhaRspF50%2B0A%2B113T3jjiAYoLZiVoCFXGMoFGk%3D&amp;reserved=0" target="_blank">here</a> to see a live demo and to learn how to implement this process. <span style="color:#FF0000"><strong>Resources designed to support you and your students when remote proctoring are available under the Resources menu, Video Chat Proctoring (e.g., Zoom, Webex).</strong></span>&nbsp;&nbsp;</span></span></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">Further options for online remote and live proctoring to be shared in the coming weeks. <strong>Please </strong><strong>email your&nbsp;<a href="https://secure-media.collegeboard.org/accuplacer/pdf/accuplacer-outreach-contacts-state.pdf?SFMC_cid=EM84728-&amp;rid=67546632" target="_blank">Outreach contact</a>&nbsp;if you have additional questions.</strong></span></span></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif"><strong>NOTE:</strong></span></span><span style="font-family:lucida sans unicode,lucida grande,sans-serif"><strong>&nbsp;</strong><span style="font-size:16px">For updates and information on ATB during these times, please log-in to your ATB test site for the most recent information.</span></span></p>

<hr>
<p style="text-align:center"><strong><span style="font-size:18px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">ACCUPLACER Ordering</span></span></strong></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif">Beginning April 1, 2020, ACCUPLACER’s Ordering Department will be <strong>closing early on Wednesdays at 4:30 p.m., EST&nbsp;</strong>until further notice. &nbsp;All other days will maintain normal operational hours from 8:30 a.m. - 6:00 p.m., EST.</span></span></p>

<hr>
<p style="text-align:center"><u><span style="font-size:18px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif"><strong>New Resources Available: Platform Enhancements and Score Web Service Client User’s Guide 3.x</strong></span></span></u></p>

<p><span style="font-size:16px"><span style="font-family:lucida sans unicode,lucida grande,sans-serif"><strong><span style="color:#FF0000">MARCH 22 UPDATE </span>–</strong> Institutions can now customize the student ISR share feature. By default, the sharing function is turned on for all institutions.&nbsp; If your institution would like to opt out from receiving ISRs from students, visit your institution profile and update accordingly.&nbsp; Additionally, at the time of testing, when students can choose up to 3 institution(s) to send their score to, their home institution will no longer appear as an option.</span></span></p>

<p><span style="font-family:lucida sans unicode,lucida grande,sans-serif; font-size:16px">Learn how to access these new features by&nbsp;reviewing </span><em>Platform Enhancements—February 2020</em><span style="font-family:lucida sans unicode,lucida grande,sans-serif; font-size:16px"> in Resources. We’ve also updated the Score Web Service Client User Guide to account for the latest version (3.x). This is also located in Resources.</span></p>
</div>
                  </div>
               </div>
               <!-- end widget div -->
            </div>
            <!-- end widget -->
         </article>
      </div>
      <div class="modal-footer">
         <div class="pull-left">
            <div class="smart-form">
               <label class="checkbox ng-binding" ng-show="updateWhatsNewFlag == 'failure'" style="">
                  <input type="checkbox" id="blockWhatsnewModal" value="Y" name="blockWhatsnewModal" class="ng-pristine ng-valid ng-untouched ng-empty" ng-true-value="'Yes'" ng-false-value="'No'" ng-model="whatsNew.checkValue">
                  <i></i>
                  Do not display this notification again until a new notice is posted.
               </label>
            </div>
         </div>
         <button type="button" ng-show="updateWhatsNewFlag == 'failure'" translate="submit" class="btn btn-sm btn-success ng-scope" ng-click="updateShowWhatsNewFlag()" style="">Submit</button>
         <button type="button" class="btn btn-default ng-scope" data-dismiss="modal" ng-click="clearMessages()" translate="common.btn.close">Close</button>
      </div>
   </div>
</div>

Вот XPATH для флажка и отправки Кнопка:

Флажок

//*[@id="whatsNewModal"]/div/div/div/div[2]/div[3]/div/div/label/i

Кнопка отправки

//*[@id="whatsNewModal"]/div/div/div/div[2]/div[3]/button[1]

1 Ответ

0 голосов
/ 08 мая 2020

Чтобы нажать на CheckBox, попробуйте это

elementchekbox = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "blockWhatsnewModal")))
elementchekbox.click()

Чтобы нажать кнопку отправки, попробуйте это

btnSubmit = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Submit']")))
btnSubmit.click()
...