#这是封装代码,跟老师的一样
def switch_windows(self,title_name=None):
handle_list = self.driver.window_handles
current_handle = self.driver.current_window_handle
for i in handle_list:
if i != current_handle:
time.sleep(1)
self.driver.switch_to.window(i)
if self.assert_title(‘title_name’):
break
time.sleep(2)
self.driver.find_element_by_id(‘userId’).send_keys(‘test’)
time.sleep(2)
#执行代码:
selfnium_driver = SeleniumDriver(‘chrome’)
print(selfnium_driver.open_url_is_true(‘http://www.imooc.com’,‘程序员’))
time.sleep(60)
selfnium_driver.switch_windows(‘网站连接’)
time.sleep(3)
selfnium_driver.close_driver()
错误打印信息:
(base) E:\Anaconda3\vscode>E:/Anaconda3/python.exe e:/Anaconda3/vscode/open_browser.py
DevTools listening on ws://127.0.0.1:14572/devtools/browser/58fa8475-51a2-4199-82d6-f7fda374d4ba
libpng warning: iCCP: cHRM chunk does not match sRGB
True
libpng warning: iCCP: cHRM chunk does not match sRGB
Traceback (most recent call last):
File “e:/Anaconda3/vscode/open_browser.py”, line 86, in
selfnium_driver.switch_windows(‘网站连接’)
File “e:/Anaconda3/vscode/open_browser.py”, line 78, in switch_windows
self.driver.find_element_by_id(‘userId’).send_keys(‘test’)
File “E:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File “E:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 978, in find_element
’value’: value})[‘value’]
File “E:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “E:\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:“css selector”,“selector”:"[id=“userId”]"}
(Session info: chrome=78.0.3904.87)
[13292:18728:1104/105703.102:ERROR:gcm_channel_status_request.cc(145)] GCM channel request failed.