appium+python自動化98-非select彈出選擇框定位解決

來源:互聯網
上載者:User

標籤:element   pdo   src   fir   自動   code   exception   position   put   

前言

遇到問題:document.getElementsByClassName(...)[0] is undefined
選擇框如果是select標籤的,可以直接用select專用的方法去錨點擊操作。其它不是select選擇框的時候,那就按正常操作步驟先點輸入框,再點選項就行了。
可是有些選擇框就是不聽話,你會發現用selenium死活定位不到,這個時候只能用萬能的js來解決了。

input選擇框

1.先看下彈出框的常見,如下這種

2.查看元素屬性,是input標籤,並且是readonly屬性,說明不能被輸入

js調試

1.首先嘗試了selenium的定位方法,發現點輸入框是可以彈出選項的,只是點選項死活點不了。於是在瀏覽器用js去調試

先點輸入框,讓它彈出選項

元素屬性<input class="el-input__inner" type="text" autocomplete="off" placeholder="請選擇" readonly="readonly">

document.getElementsByClassName(‘el-input__inner‘)[2].click();

2.等選項彈出來了,再次在瀏覽器輸入js去點選項

元素屬性<ul class="el-scrollbar__view el-select-dropdown__list" style="position: relative;">    <li class="el-select-dropdown__item hover">        <span>車贏銀行</span>

document.getElementsByClassName(‘hover‘)[0].click();

3.於是用selenium執行js,部分參考代碼如下

js1 = "document.getElementsByClassName('el-input__inner')[2].click();"self.driver.execute_script(js1)time.sleep(1)js2 = "document.getElementsByClassName('hover')[0].click();"self.driver.execute_script(js2)

運行報錯:

selenium.common.exceptions.WebDriverException: Message: document.getElementsByClassName(...)[0] is undefined

看這個報錯後反覆檢查了文法,發現沒毛病,並且反覆在瀏覽器調試,也沒問題,差點懷疑人生了!!!後來發現是前面一個js執行後失去了焦點,導致第二個js找不到焦點了

移動滑鼠

1.當元素失去焦點後,死後定位不到,這個就好比你在瀏覽器上瀏覽小電影的時候,突然有個人發給QQ抖動視窗,此時你想繼續瀏覽小網站,你需要重新點下網頁,讓滑鼠聚集在網頁上才能操作。
解決辦法:把滑鼠重新移過去

2.使用ActionChains移動滑鼠到需要點擊的元素上,參考代碼

from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport time# ** 上海-悠悠 QQ交流群:588402570**driver=webdriver.Firefox()# 省略中間步驟js1 = "document.getElementsByClassName('el-input__inner')[2].click();"self.driver.execute_script(js1)time.sleep(1)el = driver.find_element_by_xpath("//*[text()='車贏銀行']")ActionChains(self.driver).move_to_element(el).perform()js2 = "document.getElementsByClassName('hover')[0].click();"self.driver.execute_script(js2)

seleniumQQ群:646645429

appium+python自動化98-非select彈出選擇框定位解決

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.