一、場景:登錄模塊為彈窗,定位不到元素。排查只有一個句柄,也非driver.switch_to_alert()問題。所以認真查看元素發現最上方有一層iframe導致定位不到元素。
解決方案:
對於iframe結構的網頁,如:顯示彈窗,必須先切入到iframe才能獲得其中的元素,然后正常操作定位元素(該干嘛干嘛),
當要獲取 iframe 外部或者跳轉了頁面必須退出iframe,否則后續怎么定位元素都是報錯。
定位iframe並切入→
方法:driver.switch_to.frame()
例子:browser.switch_to.frame(browser.find_element_by_id('popup_login_frame'))
退出iframe→
方法:
driver.switch_to.default_content()
二、場景:定位a標簽點擊時一直報錯:
Traceback (most recent call last):
File "F:/1/1.py", line 22, in <module>
browser.find_element_by_link_text(u'管理中心').click() #點擊a標簽
File "C:\Users\4399-1500\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\4399-1500\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\4399-1500\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\4399-1500\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a class="a1" href="javascript:;"> is not clickable at point (466.5,47) because another element <div id="loginBg"> obscures it
解決方案:
一開始以為是元素定位不到,使用各種方法均失敗,最終發現點擊時被其他元素遮擋了,所以只要做休眠即可成功點擊(So easy。。。)