如何用Selenium Python切换标签页后向Angular输入框输入文本
问题分析与解决方案
出现的异常
AttributeError: 'tuple' object has no attribute 'send_keys'ElementClickInterceptedExceptionNoSuchElementException- 附加问题:
click()不再被识别为方法,显示为click: Any
目标输入框HTML代码
<fieldset> <label>Username:</label> <input ng-required="true" ng-readonly="!hasPriviledgeToEdit(user)" ng-model="user.loginId" ng-pattern="/^[a-zA-Z0-9_]+$/" name="loginId" maxlength="16" class="ng-pristine ng-untouched ng-invalid ng-invalid-required ng-valid-pattern ng-valid-maxlength" required="required"> 4-16 chars; A-z, 0-9, _ <!-- ngIf: userProfileForm.loginId.$error.required --><span ng-if="userProfileForm.loginId.$error.required" class="invalid warning ng-scope">Required</span><!-- end ngIf: userProfileForm.loginId.$error.required --> <!-- ngIf: userProfileForm.loginId.$error.pattern --> </fieldset>
用户Python代码
from multiprocessing.connection import wait import time from webbrowser import Chrome from xml.etree.ElementPath import find from selenium import webdriver 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.ui import WebDriverWait from selenium.common.exceptions import NoSuchElementException 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(5)) profiledrop = driver.find_element(By.XPATH, '//*[@id="user-salutation"]') profiledrop.click() #store original window ID original_window = 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 wait.until(EC.number_of_windows_to_be(2)) #loops through till new window is confirmed open for window_handle in driver.window_handles: if window_handle != original_window: driver.switch_to.window(window_handle) break #Tells program to wait till new window confirmed wait.until(EC.title_is("Admin")) NewUsrBtn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.newUser.ng-scope[ng-if^='isTrustedAdmin'][ng-click^='onNewUser']"))) NewUsrBtn.click() UsrNM = driver.find_element(By.XPATH, "//input[@name='loginId']") UsrNM.send_keys('justin')
已尝试的定位路径
//input[@name='loginId'] input[name='loginId'] //*[@id="data"]/div[3]/table/tbody[1]/tr/td[1]/ng-form/fieldset[1]/input #data > div.fixed-table-container-inner > table > tbody:nth-child(2) > tr > td:nth-child(1) > ng-form > fieldset:nth-child(1) > input
解决方案
1. 确保驱动稳定指向新标签页
- 不要仅依赖标题确认页面加载,同时等待新页面的关键交互元素出现,避免页面未完全渲染就执行操作:
# 等待新窗口打开 wait.until(EC.number_of_windows_to_be(2)) # 切换到新窗口并验证页面加载 for handle in driver.window_handles: if handle != original_window: driver.switch_to.window(handle) # 等待新页面的核心元素加载(比如表单容器) wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "ng-form"))) break - 避免使用
driver.window_handles[1]这种索引定位,窗口顺序可能受浏览器行为影响,遍历对比原始窗口句柄更可靠。
2. 正确定位Angular输入框
Angular元素优先使用框架特有属性定位,避免依赖易变的结构路径:
- 推荐XPATH:
//input[@ng-model="user.loginId"](绑定的user.loginId是业务唯一标识,比name更稳定) - 推荐CSS选择器:
input[ng-model="user.loginId"] - 必须结合显式等待确保元素可交互,不要直接用
driver.find_element:UsrNM = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.XPATH, "//input[@ng-model='user.loginId']")) ) UsrNM.send_keys('justin') - 绝对路径(如带
tbody[1]、div[3]的定位器)废弃,页面结构调整会直接导致定位失效。
3. 异常针对性修复
AttributeError: 'tuple' object has no attribute 'send_keys':检查是否误将定位参数以元组形式直接赋值(比如漏写find_element),确保最终获取的是WebElement对象;ElementClickInterceptedException:所有点击操作都用EC.element_to_be_clickable等待,确保元素未被遮罩层、弹窗遮挡;click: Any:此问题源于定位失败,返回的不是WebElement,只要确保元素定位正确,该提示会自动消失。
内容的提问来源于stack exchange,提问作者DaHawg
相关产品推荐
相关产品推荐

