1. 程式人生 > 其它 >填坑webUI自動化-下拉選擇元素

填坑webUI自動化-下拉選擇元素

看著是readyonly,嘗試js移除readonly屬性,然後直接send_keys

# 輸入元素 下拉框方法封裝 #直接sendkeys不行,不會往下走
def base_send_keys(self, loc, value):
# 先找到元素
el = self.base_find(loc)
self.driver.execute_script("document.getElementsByTagName('input')[0].removeAttribute('readonly');")
self.driver.execute_script("document.getElementsByTagName('input')[1].removeAttribute('readonly');")
# 輸入
el.send_keys(value)
send是send進去了,但是帶不出來下一個NOSTRO CODE的值,要點選一下才能觸發去帶出來對應的值

好換一種:
# 選擇下拉元素方法封裝
def base_select(self, loc,value):
Select(self.driver.find_element_by_xpath(loc)).select_by_visible_text(value)
#Message: Select only works on <select> elements, not on <li>
確實不是個正經的下拉框

正常還可以用actionchain,不過也失敗了,程式碼被我改過了,不粘了

最後寫了個demo

def test_demo(self):
options = Options()
options.add_experimental_option ("debuggerAddress", "127.0.0.1:9222")
self.driver = webdriver.Chrome(options = options)
_iframe_loc = self.driver.find_elements(By.CSS_SELECTOR,'.iframe')#//*[@id="pane-#CBIBANK"]/iframe
self.driver.switch_to.frame(_iframe_loc[1])
self.driver.find_element_by_xpath("//*[@class='el-input__inner']").click()
# els = self.driver.find_elements_by_xpath('//li[@class="el-select-dropdown__item"]')
# for el in els:
# # print(el.get_attribute("outerText"))
# if "SIGNATURE BANK" in el.text:
# el.click()
el = self.driver.find_element_by_xpath('//li[@class="el-select-dropdown__item"]/span[contains(text(),"SIGNATURE BANK")]')
print(el)
self.driver.execute_script("arguments[0].click();",el)
# # print(els)

發現先找到元素,然後用js點選是可行的
#下拉框找到元素,js,click
def base_js_click(self,el):
self.driver.execute_script("arguments[0].click();",el)