How to use switch_to_last_window method in Airtest

Best Python code snippet using Airtest

XXQG_V2_server.py

Source:XXQG_V2_server.py Github

copy

Full Screen

...53 if article_index_start < len(articles): 54 article = articles[article_index_start]55 article_index_start += 156 article.click()57 mybrowser.switch_to_last_window()58 mybrowser.auto_read(article_page, article_index_start, action, delay_steps, delay_per_step)59 mybrowser.close_and_switch_to_last_window()60 else:61 break62 articles[0].click()63 mybrowser.switch_to_last_window()64 point = mybrowser.get_point()65 mybrowser.close_and_switch_to_last_window()66 else:67 btns = mybrowser.find_elements(By.CLASS_NAME, 'btn')68 for btn in btns:69 if btn.text == '>>':70 btn.click()71 article_index_start = 072 article_page += 173 while point.article_time < 6:74 print('[{}] Reading article for time ...'.format(datetime.now()))75 articles[0].click()76 mybrowser.switch_to_last_window()77 delay_steps = 6 * (6 - point.video_time)78 delay_per_step = 2079 mybrowser.auto_read(article_page, 0, action, delay_steps, delay_per_step)80 point = mybrowser.get_point()81# 观看视频82if point.video_watched + point.video_time < 12:83 while len(mybrowser.window_handles) > 1:84 mybrowser.close_and_switch_to_last_window()85 mybrowser.get(url_main)86 # while mybrowser.current_url != url_main:87 # mybrowser.close_and_switch_to_last_window()88 mybrowser.wait_and_click(sel.XPATH, '//*[@id="root"]/div/header/div[2]/div[1]/div[2]/a[2]')89 mybrowser.wait_and_click(sel.XPATH, '//*[@id="495f"]/div/div/div/div/div/section/div/div/div/div[1]/div[1]/div/div')90 mybrowser.wait(sel.CLASS, 'innerPic')91 video_index_start = 092 video_page = 093 while point.video_watched < 6:94 if video_index_start < 20:95 if video_index_start == 0:96 videos = mybrowser.find_elements(By.CLASS_NAME, 'innerPic')97 for _ in range(6 - point.video_watched):98 if video_index_start < len(videos): 99 video = videos[video_index_start]100 video_index_start += 1101 video.click()102 mybrowser.switch_to_last_window()103 if not HEADLESS: 104 mybrowser.wait_for_video_and_mute()105 watch_time = 5 + random() * 3106 print('[{}] Watching video, page = {}, index = {}, time = {}s'.format(datetime.now(), video_page, video_index_start, watch_time))107 sleep(watch_time)108 mybrowser.close_and_switch_to_last_window()109 else:110 break111 video = videos[0]112 video.click()113 mybrowser.switch_to_last_window()114 point = mybrowser.get_point()115 mybrowser.close_and_switch_to_last_window()116 else:117 btns = mybrowser.find_elements(By.CLASS_NAME, 'btn')118 for i in btns:119 if i.text == '>>':120 i.click()121 video_index_start = 0122 video_page += 1123 break124 while point.video_time < 6:125 print('[{}] Watching video for time ...'.format(datetime.now()))126 mybrowser.wait_and_click(sel.XPATH, '//*[@id="0454"]/div/div/div/div/div/div/div/div[1]/div/div[2]/div[5]/div/div')127 mybrowser.wait_and_click(sel.CLASS, 'innerPic')128 if not HEADLESS:129 mybrowser.wait_for_video_and_mute()130 sleep((6 - point.video_time) * 250)131 point = mybrowser.get_point()132 mybrowser.close_and_switch_to_last_window()133point = mybrowser.get_point()134mybrowser.close_all()135print('[{}] 刷分完成'.format(datetime.now()))136msg = '每日登录:{}/1\t\n'.format(point.login)137msg += '阅读文章:{}/6\t\n'.format(point.article_read)138msg += '视听学习:{}/6\t\n'.format(point.video_watched)139msg += '文章时长:{}/6\t\n'.format(point.article_time)140msg += '视听时长:{}/6\t\n'.format(point.video_time)...

Full Screen

Full Screen

XXQG_V2.py

Source:XXQG_V2.py Github

copy

Full Screen

...39 if article_index_start < len(articles): 40 article = articles[article_index_start]41 article_index_start += 142 article.click()43 mybrowser.switch_to_last_window()44 mybrowser.auto_read(article_page, article_index_start, action, delay_steps, delay_per_step)45 mybrowser.close_and_switch_to_last_window()46 else:47 break48 articles[0].click()49 mybrowser.switch_to_last_window()50 point = mybrowser.get_point()51 mybrowser.close_and_switch_to_last_window()52 else:53 btns = mybrowser.find_elements(By.CLASS_NAME, 'btn')54 for btn in btns:55 if btn.text == '>>':56 btn.click()57 article_index_start = 058 article_page += 159 while point.article_time < 6:60 print('[{}] Reading article for time ...'.format(datetime.now()))61 articles[0].click()62 mybrowser.switch_to_last_window()63 delay_steps = 6 * (6 - point.video_time)64 delay_per_step = 2065 mybrowser.auto_read(article_page, 0, action, delay_steps, delay_per_step)66 point = mybrowser.get_point()67# 观看视频68if point.video_watched + point.video_time < 12:69 while len(mybrowser.window_handles) > 1:70 mybrowser.close_and_switch_to_last_window()71 mybrowser.get(url_main)72 # while mybrowser.current_url != url_main:73 # mybrowser.close_and_switch_to_last_window()74 mybrowser.wait_and_click(sel.PARTIAL_LINK, '学习电视台')75 mybrowser.wait_and_click(sel.XPATH, '//*[@id="495f"]/div/div/div/div/div/section/div/div/div/div[1]/div[1]/div/div')76 mybrowser.wait(sel.CLASS, 'innerPic')77 video_index_start = 078 video_page = 079 while point.video_watched < 6:80 if video_index_start < 20:81 if video_index_start == 0:82 videos = mybrowser.find_elements(By.CLASS_NAME, 'innerPic')83 for _ in range(6 - point.video_watched):84 if video_index_start < len(videos): 85 video = videos[video_index_start]86 video_index_start += 187 video.click()88 mybrowser.switch_to_last_window()89 mybrowser.wait_for_video_and_mute()90 watch_time = 5 + random() * 391 print('[{}] Watching video, page = {}, index = {}, time = {}s'.format(datetime.now(), video_page, video_index_start, watch_time))92 sleep(watch_time)93 mybrowser.close_and_switch_to_last_window()94 else:95 break96 video = videos[0]97 video.click()98 mybrowser.switch_to_last_window()99 point = mybrowser.get_point()100 mybrowser.close_and_switch_to_last_window()101 else:102 btns = mybrowser.find_elements(By.CLASS_NAME, 'btn')103 for i in btns:104 if i.text == '>>':105 i.click()106 video_index_start = 0107 video_page += 1108 break109 while point.video_time < 6:110 print('[{}] Watching video for time ...'.format(datetime.now()))111 mybrowser.wait_and_click(sel.XPATH, '//*[@id="0454"]/div/div/div/div/div/div/div/div[1]/div/div[2]/div[5]/div/div')112 mybrowser.wait_and_click(sel.CLASS, 'innerPic')113 mybrowser.wait_for_video_and_mute()114 sleep((6 - point.video_time) * 250)115 point = mybrowser.get_point()116 mybrowser.close_and_switch_to_last_window()117point = mybrowser.get_point()118mybrowser.close_all()119print('[{}] 刷分完成'.format(datetime.now()))120msg = '每日登录:{}/1\t\n'.format(point.login)121msg += '阅读文章:{}/6\t\n'.format(point.article_read)122msg += '视听学习:{}/6\t\n'.format(point.video_watched)123msg += '文章时长:{}/6\t\n'.format(point.article_time)124msg += '视听时长:{}/6\t\n'.format(point.video_time)...

Full Screen

Full Screen

myDriver.py

Source:myDriver.py Github

copy

Full Screen

...26 def wait_for_video_and_mute(self):27 video = self.find_element_by_css_selector("video")28 self.execute_script("arguments[0].muted = true;", video)29 30 def switch_to_last_window(self):31 self.switch_to_window(self.window_handles[-1])32 def close_and_switch_to_last_window(self):33 self.close()34 self.switch_to_window(self.window_handles[-1])35 def close_all(self):36 self.switch_to_last_window()37 if self.current_window_handle != self.window_handles[0]:38 self.close_and_switch_to_last_window()39 else:40 self.close()41 def match_selector(self, selector):42 if selector == sel.CSS:43 By_sel = By.CSS_SELECTOR44 elif selector == sel.XPATH:45 By_sel = By.XPATH46 elif selector == sel.PARTIAL_LINK:47 By_sel = By.PARTIAL_LINK_TEXT48 elif selector == sel.CLASS:49 By_sel = By.CLASS_NAME50 return By_sel51 def wait_and_click(self, selector, location):52 s = self.match_selector(selector)53 element = WebDriverWait(self, 60).until(EC.presence_of_element_located((s, location)))54 element.click()55 self.switch_to_window(self.window_handles[-1])56 def wait(self, selector, location):57 s = self.match_selector(selector)58 WebDriverWait(self, 60).until(EC.presence_of_element_located((s, location)))59 60 def login(self):61 QR_XPATH = '//*[@id="ddlogin-iframe"]'62 print('Getting login page ...')63 self.get(url_main)64 self.wait_and_click(sel.CSS, '.login-icon')65 self.switch_to_last_window()66 # self.execute_script('window.scrollTo(0,document.body.scrollHeight)')67 self.wait(sel.XPATH, QR_XPATH)68 self.execute_script("arguments[0].scrollIntoView();", self.find_element(By.XPATH, QR_XPATH))69 url_QR = self.find_element(By.XPATH, QR_XPATH)70 login_QR = url_QR.screenshot('./login_QR.png')71 print('Save QR code to ./login_QR.png')72 while self.current_url != url_main: pass73 self.close_and_switch_to_last_window()74 def get_point(self):75 print('[{}] Checking point ...'.format(datetime.now()))76 if self.current_url != url_main:77 self.get(url_main)78 self.switch_to_last_window()79 self.wait_and_click(sel.XPATH, '//*[@id="root"]/div/div/section/div/div/div/div/div[4]/section/div[4]')80 self.switch_to_last_window()81 self.wait(sel.XPATH, '//*[@id="app"]/div/div[2]/div/div[2]/div[2]/span[1]')82 sleep(1)83 element = self.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/div/div[2]/div[2]/span[1]')84 points = point()85 points.total = int(element.text)86 pointCards = self.find_elements(By.CLASS_NAME, 'my-points-card-text')87 [points.login, point.article_read, points.video_watched, points.article_time, points.video_time] = [int(i.text[0]) for i in pointCards]88 self.close_and_switch_to_last_window()89 return points90 def auto_read(self, articlr_page, article_index, action, delay_steps, delay_per_step):91 print('[{}] Reading article, page = {}, index = {}'.format(datetime.now(), articlr_page, article_index))92 for _ in range(delay_steps):93 action.perform()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful