1.頁面彈出框的處理
def webdriverwait_baidu_alert(self): ''' 頁面彈出框獲取: 1.獲取登錄元素定位 2.顯性等待 3.直到登錄彈出框出現 ''' self.driver.find_element_by_xpath('//div[@id="u1"]//a[@name="tj_login"]').click() #進行顯性等待,使得操作的元素可見 ele_locator = "TANGRAM__PSP_10__footerULoginBtn" param = (By.ID,ele_locator) WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(param)) #再去操作元素 self.driver.find_element_by_id(ele_locator).click() #此時頁面又發生變化,還需要等待,使得操作的元素可見 ele_locator1 = "TANGRAM__PSP_10__userName" param1 = (By.ID,ele_locator1) WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(param1)) #再去獲取元素並進行輸入 self.driver.find_element_by_id(ele_locator1).send_keys("1234567") self.driver.find_element_by_id("TANGRAM__PSP_10__password").send_keys("111111") time.sleep(10)
2.alert彈出框的處理
def webdriverwait_windows_alert(self): ''' windows彈出框: 1.獲取頁面元素,用顯性等待等到alert彈框出現 2.切換到windows彈出框alert 3.操作alert彈框選擇yes或者no ''' self.driver.find_element_by_xpath('//input[@value="提交"]').click() WebDriverWait(self.driver,10).until(EC.alert_is_present()) #從html頁面切換到alert頁面 alert_1 = self.driver.switch_to.alert #獲取alert的文本內容 print(alert_1.text) time.sleep(4) #接收選擇ok alert_1.accept()