测试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))