selenium 獲取按鈕的筆記


測試odoo時,發現大部分按鈕都是動態生成,id也是動態的,

只能用xpath,但是配置一旦改變導致按鈕位置改變

想到一個辦法,遍歷所有按鈕,然后內容相符的才點擊,測試代碼如下

submit_loc = (By.XPATH,"/html/body/div/div/form/div[3]/button")
self.find_element(*self.submit_loc).click()
login_value = self.find_element(*self.submit_loc).get_attribute("textContent")
print(login_value)

最后整理到基類里
    def click_button_byText(self, text):
        try:
            # WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
            # time.sleep(1)
            # return self.driver.find_elements(*loc)
            elements = self.find_elements('tag name', 'button')
            # print(type(submits))
            for i in elements:
                # print(type(i))
                print(i.get_attribute("textContent"))
                if i.get_attribute("textContent").find(text)>= 0:
                    i.click()
                    break
        except AttributeError:
            print(u"%s 頁面中未能找到 %s 元素" % (self, text))

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM