我正在用Selenium创建一个自动化脚本,以便能够在我的公司使用的网站上创建用户帐户。该程序从active目录中提取所有用户信息,并通过selenium传递信息以创建帐户。如果创建了一个重复的帐户,网站将抛出一个错误。当它这样做时,我希望selenium能够识别错误,并在用户名的末尾添加一个数字,如果它不抛出一个错误,我希望它忽略并继续。我试着用IF trying语句来做这件事。我正在运行的问题是,当错误初始化时,包含错误的HTML元素总是可见的,只是HTML中的一个小变化。我尝试编写XPATH以捕获更改,但始终得到一个TimeoutException错误。我在下面添加了所有脚本,以及在抛出错误之前和抛出错误之后显示的两个HTML元素。如果您对处理错误的更好方法有任何建议,也将受到极大的赞赏。
抛出前错误的HTML
<div class="error-section ng-hide" ng-show="hasErrors">
<button class="close" ng-click="clearErrors()"></button>
<p>Sorry, something unexpected happened. Please try again.</p>
<ul class="nostyle">
<!-- ngRepeat: error in errorMessages -->
</ul>
</div>抛出错误的HTML
<div class="error-section" ng-show="hasErrors">
<button class="close" ng-click="clearErrors()"></button>
<p>Sorry, something unexpected happened. Please try again.</p>
<ul class="nostyle">
<!-- ngRepeat: error in errorMessages --><li ng-repeat="error in errorMessages" class="ng-binding ng-scope">Code: 102000890</li><!-- end ngRepeat: error in errorMessages --><li ng-repeat="error in errorMessages" class="ng-binding ng-scope">FaultCode: InvalidData</li><!-- end ngRepeat: error in errorMessages --><li ng-repeat="error in errorMessages" class="ng-binding ng-scope">Error: 102000890 - Login Id is already in use. Please create a different login Id.</li><!-- end ngRepeat: error in errorMessages --><li ng-repeat="error in errorMessages" class="ng-binding ng-scope">Details: </li><!-- end ngRepeat: error in errorMessages -->
</ul>
</div>完整脚本
import time
from hashlib import new
from multiprocessing.connection import wait
from select import select
from webbrowser import BaseBrowser, Chrome
from xml.etree.ElementPath import find
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
import ADTest
driver = webdriver.Chrome()
driver.get('https://power.dat.com')
# chrome_optinons = webdriver.ChromeOptions()
# chrome_optinons.add_argument('--headless')
# chrome_optinons.add_argument('window-size=1920x1080')
#setup wait
wait = WebDriverWait(driver, 10)
username = driver.find_element(By.XPATH, '//*[@id="mat-input-1"]')
username.send_keys('')
password = driver.find_element(By.XPATH, '//*[@id="mat-input-0"]')
password.send_keys('')
loginbutton = driver.find_element(By.XPATH, '//*[@id="submit-button"]')
loginbutton.click()
(time.sleep(2))
profiledrop = driver.find_element(By.XPATH, '//*[@id="user-salutation"]')
profiledrop.click()
#store original window ID
windows_before = driver.current_window_handle
# #checks no other windows open
# assert len(driver.window_handles) == 1
Adminbutton = driver.find_element(By.CSS_SELECTOR, '#userPreferencesUl > li:nth-child(5) > a')
Adminbutton.click()
# driver.switch_to.window(driver.window_handles[1])
#Wait for new window Tab
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
windows_after = driver.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
driver.switch_to.window(new_window)
#Tells program to wait till new window confirmed
WebDriverWait(driver,20).until(EC.title_is("Admin"))
(time.sleep(5))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.newUser.ng-scope[ng-if^='isTrustedAdmin'][ng-click^='onNewUser']"))).click()
#NewUsrBtn.click()
#User Name
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-invalid ng-invalid-required ng-valid-pattern ng-valid-maxlength' and @name='loginId']"))).send_keys("TQL" + ADTest.DatUsrName)
#UsrNM.send_keys('justin')
#<input ng-required="true" ng-readonly="!hasPriviledgeToEdit(user)" ng-model="user.loginId" ng-pattern="/^[a-zA-Z0-9_]+$/" name="loginId" maxlength="16" class="ng-valid-pattern ng-valid-maxlength ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" required="required">
#Password
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-invalid ng-invalid-required ng-valid-pattern ng-valid-maxlength' and @name='password']"))).send_keys("total2")
#Confirm Password
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='confirmPassword']")))
element.send_keys("total2")
#State
state = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='ng-pristine ng-untouched ng-invalid ng-invalid-required' and @name='state']")))
selectState = Select(state)
selectState.select_by_value('string:' + ADTest.DATST)
#First Name
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-invalid ng-invalid-required ng-valid-maxlength' and @name='firstName']"))).send_keys(ADTest.FirstN)
#Last Name
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-invalid ng-invalid-required ng-valid-maxlength' and @name='lastName']"))).send_keys(ADTest.LastN)
#Workgroup ID
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-invalid ng-invalid-required ng-valid-maxlength' and @name='initials']"))).send_keys(ADTest.DATint)
#Phone Number
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-invalid ng-invalid-required ng-valid-pattern ng-valid-maxlength' and @name='primaryPhone']"))).send_keys("8005803101")
#Extension
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-valid ng-valid-pattern ng-valid-maxlength' and @name='primaryExtension']"))).send_keys(ADTest.ext)
#Email
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-untouched ng-valid-email ng-invalid ng-invalid-required ng-valid-maxlength' and @name='email']"))).send_keys(ADTest.email)
#National Account: Combo Premium Pooling Subscription
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(text(),'National Account: Combo Premium Pooling Subscripti')]"))).click()
#National Account: Combo Premium Pooling Subscription
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[normalize-space()='National Account: DAT Connexion Subscription']"))).click()
#National Account: Combo Premium Pooling Subscription
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[normalize-space()='National Account: Power Broker Subscription']"))).click()
#Submit Button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Add User']"))).click()
if WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@class= "error-section" and @id="data"]/div[1]'))):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="data"]/div[3]/table/tbody[1]/tr/td[1]/ng-form/fieldset[1]/input'))).send_keys("1")
# WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="data"]/div[3]/table/tbody[1]/tr/td[1]/ng-form/fieldset[1]/input'))).send_keys("1")
# WebDriverWait(driver,20).until(EC.element_to_be_clickable(By.XPATH, '//*[@id="data"]/div[1]/button')).click()
else:
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@class= "error-section ng-hide" and @id="data"]/div[1]')))发布于 2022-08-09 19:56:34
要处理可能的错误消息,可以将单击Close按钮封装在try-except{}块中,从而为诱导WebDriverWait,您可以使用以下任何一个
使用
尝试:WebDriverWait(驱动程序,WebDriverWait打印(“关闭错误消息”),但TimeoutException: print(“无错误消息”)除外
使用XPATH的
尝试:WebDriverWait(驱动程序,WebDriverWait "//button@class='close‘并以(@ng-click,’clearErrors‘“)开头).click()打印(”关闭错误消息“),但TimeoutException: print(”无错误消息“)除外
https://stackoverflow.com/questions/73295327
复制相似问题