在進行自動化測試的測試時遇到了這個報錯信息
經過一番百度之后意識到是圖層遮擋的問題
Selenium可以切換iframe卻不可以操作頁面圖層(可能是我不會吧)
但是卻找到了js操作網頁圖層的方法
然后剛好想到Selenium中可以運行js腳本片段(這就很搭了(✪ω✪))
錯誤信息:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a href="#" class="prefpanelgo">...</a> is not clickable at point (580, 40). Other element would receive the click: <label for="sh_1">...</label>
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.16299 x86_64)
1.Selenium定位的元素被圖層遮擋了的解決辦法
針對遮擋問題
# 定位到要操作的元素 el = driver.find_element_by_css_selector('[class="prefpanelgo"]') #代碼如下就點擊到點擊到定位的元素了 driver.execute_script("arguments[0].click();", el)
# 一般圖層就是點擊用,要做其他的話就把click方法和元素對象刪除了就可以吧(我猜的)
# arguments[0]應該就是切換到最上層的圖層(沒錯也是我猜噠!)
相當好用,因為直接注入js腳本,就不是在屏幕上操作了,而是js操作,神級操作啊
2.Selenium中寫js進行頁面的下滑操作
# x是水平方向,y是垂直方向單位是像素px js = 'window.scrollTo(0,1000)' driver.execute_script(js)
3.Selenium中寫js讓頁面向上滑動
# scrollTop=800就是滑動塊距離頂部的距離值px # scrollTop=0就是回到頂部 js = 'var q=document.documentElement.scrollTop=800' driver.execute_script(js)
遇到問題的時候就說明你要學習了!加油,在禿頭之路上越走越遠!!