Problem Selecting Okay Button with Python and Selenium

為{幸葍}努か 提交于 2021-01-28 19:33:55

问题


So I have an issue, where I am unable to select the okay from an alert message that pops up in a new window. I don't really know what to make of it and really just want to accept the alert as all of the work has been done to ensure that the removal is processed. The code for the overall script can be found here: https://github.com/Richard-Barrett/ITDataServicesInfra/blob/master/Python/Skyward/Administration/remove_sec_groups_inactive_users.py

Code:

#!/bin/usr/env python
# ===========================================================
# Created By: Richard Barrett
# Organization: DVISD
# DepartmenT: Data Services
# Purpose: Skyward Administration
# Date: 04/01/2020
# ===========================================================

import selenium
import shutil
import xlsxwriter
import os
import unittest
import requests
import subprocess
import getpass
import platform
import pynput
import logging
import time 
from pynput.keyboard import Key, Controller
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait 
from datetime import date

decrypt = "gpg --output secrets.json --decrypt secrets.gpg" 

if os.path.exists("secrets.gpg"):
      returned_value = subprocess.call(decrypt, shell=True)
else:
        print("The file does not exist!")
        print("You should probably create a secret!")
        print("gpg --output filename.gpg --encrypt filename.json")

import json
with open('secrets.json','r') as f:
      config = json.load(f)

# Definitions
# find_elements_by_name
# find_elements_by_xpath
# find_elements_by_link_text
# find_elements_by_partial_link_text
# find_elements_by_tag_name
# find_elements_by_class_name
# find_elements_by_css_selector

# System Variables
today = date.today()
date = today.strftime("%m/%d/%Y")
node = platform.node()
system = platform.system()
username = getpass.getuser()
version = platform.version()
keyboard = Controller()

# Upload Path Variables 
file_input_inactive_users = os.path.abspath("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")

# URL Variables 
login_url = ''
redirect_url = ''
reports_scheduler_url = ''
custom_reports_url = ''

# Check for Version of Chrome

# WebDriver Path for System
if platform.system() == ('Windows'):
    browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
elif platform.system() == ('Linux'):
    browser = webdriver.Chrome(executable_path='/home/rbarrett/Drivers/Google/Chrome/chromedriver_linux64/chromedriver')
elif platform.system() == ('Darwin'):
    browser = webdriver.Chrome(executable_path='~/Drivers/Google/Chrome/chromedriver_mac64/chromedriver')
else:
    print("Are you sure you have the Selenium Webdriver installed in the correct path?")

# tearDown Method
def tearDown(self):
    self.browser.close()

# shutDown Method 
def shutDown(self):
    self.browser.quit()

# Parent URL
browser.get("https://skyward-student.del-valle.k12.tx.us/scripts/wsisa.dll/WService=wsEAplus/seplog01.w?nopopup=true")
time.sleep(5)

# Credentials NEEDS UNIT TEST
username = browser.find_element_by_id("login")
password = browser.find_element_by_id("password")
username.send_keys(config['user']['name'])
password.send_keys(config['user']['password'])

# Authentication submit.click()
# For XPATH = //*[@id='bLogin']
element = WebDriverWait(browser, 20).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='bLogin']")))
element.click();
print("Logging into <insert_program>!")
print("Authenticated")

# Click and Span Skyward Contact Access
# Adminsitration XPATH = //*[@id='nav_ContactAccess']/span
element = WebDriverWait(browser, 20).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@id='nav_ContactAccess']/span")))
element.click();

# Click on Secured Users
# XPATH = //a[@id='nav_SecuredUsers']/span
element = WebDriverWait(browser, 20).until(
            EC.element_to_be_clickable((By.XPATH, "//a[@id='nav_SecuredUsers']/span")))
element.click();

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

# Send Keys to Lookup 
# XPATH = //*[@id='brSecuredUsersLookupInput']
target_user = WebDriverWait(browser, 20).until(
                    EC.element_to_be_clickable((By.XPATH, "//*[@id='brSecuredUsersLookupInput']")))
target_user.send_keys(config['sec_group_removal']['name_key']);
target_user.send_keys(Keys.RETURN);

# Expand Button on Element Needing Sec Group Removal
# Class "bd_open"
element = WebDriverWait(browser, 20).until(
            EC.element_to_be_clickable((By.CLASS_NAME, "bd_closed")))
element.click()

# Click on Remove All Groups By Link Text
# find_elements_by_link_text
element = WebDriverWait(browser, 20).until(
                    EC.element_to_be_clickable((By.LINK_TEXT, "Remove All Groups")))
element.click()

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

# Click Ok by ID
# XPATH //*[@id='msgBtn1'] 
element = WebDriverWait(browser, 20).until(
                            EC.element_to_be_clickable((By.XPATH, "//*[@id='msgBtn1']")))
element.click()

A message pops up and looks like this once I hit the Remove All Groups:

I think part of the code is that I am not handling it right because it is not switching to the Window:

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

Here is a copy of the element I need to click on:

<a class="button" id="msgBtn1" tabindex="5" href="javascript:if (cbs('msgBtn1')) {closeMessage(false); validateForm('xAllGroups','ssusrhttp001.w','ssusrbrws001.w'); cancelEvent();}" role="button" style="width: 100px; font-weight: normal;">OK&nbsp;</a>

回答1:


Turned out I didn't need to switch to the window. The alert was part of the main page. I took out the

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

Now the code looks like this and it works just fine:

#!/bin/usr/env python
# ===========================================================
# Created By: Richard Barrett
# Organization: DVISD
# DepartmenT: Data Services
# Purpose: Skyward Administration
# Date: 04/01/2020
# ===========================================================

import selenium
import shutil
import xlsxwriter
import os
import unittest
import requests
import subprocess
import getpass
import platform
import pynput
import logging
import time 
from pynput.keyboard import Key, Controller
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait 
from datetime import date

decrypt = "gpg --output secrets.json --decrypt secrets.gpg" 

if os.path.exists("secrets.gpg"):
      returned_value = subprocess.call(decrypt, shell=True)
else:
        print("The file does not exist!")
        print("You should probably create a secret!")
        print("gpg --output filename.gpg --encrypt filename.json")

import json
with open('secrets.json','r') as f:
      config = json.load(f)

# Definitions
# find_elements_by_name
# find_elements_by_xpath
# find_elements_by_link_text
# find_elements_by_partial_link_text
# find_elements_by_tag_name
# find_elements_by_class_name
# find_elements_by_css_selector

# System Variables
today = date.today()
date = today.strftime("%m/%d/%Y")
node = platform.node()
system = platform.system()
username = getpass.getuser()
version = platform.version()
keyboard = Controller()

# Upload Path Variables 
file_input_inactive_users = os.path.abspath("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")

# URL Variables 
login_url = ''
redirect_url = ''
reports_scheduler_url = ''
custom_reports_url = ''

# Check for Version of Chrome

# WebDriver Path for System
if platform.system() == ('Windows'):
    browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
elif platform.system() == ('Linux'):
    browser = webdriver.Chrome(executable_path='/home/rbarrett/Drivers/Google/Chrome/chromedriver_linux64/chromedriver')
elif platform.system() == ('Darwin'):
    browser = webdriver.Chrome(executable_path='~/Drivers/Google/Chrome/chromedriver_mac64/chromedriver')
else:
    print("Are you sure you have the Selenium Webdriver installed in the correct path?")

# tearDown Method
def tearDown(self):
    self.browser.close()

# shutDown Method 
def shutDown(self):
    self.browser.quit()

# Parent URL
browser.get("https://skyward-student.del-valle.k12.tx.us/scripts/wsisa.dll/WService=wsEAplus/seplog01.w?nopopup=true")
time.sleep(5)

# Credentials NEEDS UNIT TEST
username = browser.find_element_by_id("login")
password = browser.find_element_by_id("password")
username.send_keys(config['user']['name'])
password.send_keys(config['user']['password'])

# Authentication submit.click()
# For XPATH = //*[@id='bLogin']
element = WebDriverWait(browser, 20).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='bLogin']")))
element.click();
print("Logging into <insert_program>!")
print("Authenticated")

# Click and Span Skyward Contact Access
# Adminsitration XPATH = //*[@id='nav_ContactAccess']/span
element = WebDriverWait(browser, 20).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@id='nav_ContactAccess']/span")))
element.click();

# Click on Secured Users
# XPATH = //a[@id='nav_SecuredUsers']/span
element = WebDriverWait(browser, 20).until(
            EC.element_to_be_clickable((By.XPATH, "//a[@id='nav_SecuredUsers']/span")))
element.click();

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

# Send Keys to Lookup 
# XPATH = //*[@id='brSecuredUsersLookupInput']
target_user = WebDriverWait(browser, 20).until(
                    EC.element_to_be_clickable((By.XPATH, "//*[@id='brSecuredUsersLookupInput']")))
target_user.send_keys(config['sec_group_removal']['name_key']);
target_user.send_keys(Keys.RETURN);

# Expand Button on Element Needing Sec Group Removal
# Class "bd_open"
element = WebDriverWait(browser, 20).until(
            EC.element_to_be_clickable((By.CLASS_NAME, "bd_closed")))
element.click()

# Click on Remove All Groups By Link Text
# find_elements_by_link_text
element = WebDriverWait(browser, 20).until(
                    EC.element_to_be_clickable((By.LINK_TEXT, "Remove All Groups")))
element.click()

# Click Ok by ID
# XPATH //*[@id='msgBtn1'] 
element = WebDriverWait(browser, 20).until(
                            EC.element_to_be_clickable((By.XPATH, "//*[@id='msgBtn1']")))
element.click()



回答2:


The easiest way to figure out these issues is to use the Selenium IDE browser plugin. There you can manually walk through the steps of your app, save it, run it to make sure it works. This is all via simply interacting with your webs app (like recording a macro). The beauty of it, it generates the code for you and identifies the CSS / HTML selectors necessary to locate objects to interact (or to locate information to assert for automated testing)



来源:https://stackoverflow.com/questions/60982025/problem-selecting-okay-button-with-python-and-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!