5-6:实现多窗口之异常(AttributeError: 'list' object has no attribute 'click')
代码:
#coding = utf-8
from selenium import webdriver
import time
##############5-6:实现多窗口切换_start##############
driver = webdriver.Chrome()
driver.get('https://www.imooc.com/user/newlogin')
driver.find_element_by_name('email').send_keys('gj10010@163.com')
driver.find_element_by_name('password').send_keys('20200319gj')
driver.find_elements_by_class_name('moco-btn').click()
driver.get('https://www.imooc.com/user/setbindsns')
driver.find_elements_by_class_name('inner-i-box').find_elements_by_class_name('moco-btn-normal').click()
time.sleep(2)
current_handl = driver.current_window_handle
handl_list = driver.window_handles
print(handl_list)
for i in handl_list:
if i != current_handl:
time.sleep(2)
driver.switch_to_window(i)
driver.find_element_by_name('username').send_keys('13501913831')
driver.find_element_by_name('password').send_keys('1351913831gj')
driver.close()
##############5-6:实现多窗口切换_end################
结果:
DevTools listening on ws://127.0.0.1:57048/devtools/browser/2f83b3cd-583f-4513-bccd-42c34432e0d4
Traceback (most recent call last):
File "e:/30.Study/30.自动化测试/99.零基础入门 Python Web 自动化测试/10.seleniumCodePractice/202006/open_window_5-6.py", line 10, in <module>
driver.find_elements_by_class_name('moco-btn').click()
AttributeError: 'list' object has no attribute 'click'
PS E:\30.Study\30.自动化测试\99.零基础入门 Python Web 自动化测试\10.seleniumCodePractice\202006>
Traceback (most recent call last):
File "e:/30.Study/30.自动化测试/99.零基础入门 Python Web 自动化测试/10.seleniumCodePractice/202006/open_window_5-6.py", line 10, in <module>
driver.find_elements_by_class_name('moco-btn').click()
AttributeError: 'list' object has no attribute 'click'
PS E:\30.Study\30.自动化测试\99.零基础入门 Python Web 自动化测试\10.seleniumCodePractice\202006>
对策:
driver.find_elements_by_class_name('moco-btn').click()
==>
driver.find_elements_by_class_name('moco-btn')[0].click()