How to use switch_to_newest_window method in SeleniumBase

Best Python code snippet using SeleniumBase

12306.py

Source:12306.py Github

copy

Full Screen

...84 route.append(train)85 86 driver.close()87 return route88def switch_to_newest_window(driver):89 windows = driver.window_handles90 driver.switch_to.window(windows[-1])91def print_all_tickets(station_name, routes):92 print('车票情况'.center(os.get_terminal_size().columns-4, '-'))93 for index in enumerate(routes):94 route = index[1]95 print('从 {} 到 {} 可购票的车次共有 {} 趟:\n'.format(station_name[index[0]], station_name[index[0] + 1], len(route)))96 for t in route:97 print('\t车次: {:5}, 时间: {}-{}, 历时: {}'.format(t.number, t.start_t, t.end_t, t.period), end='')98 for i in range(10):99 print(', {}: {}'.format(key_list[i], t.seats[key_list[i]]), end='')100 print()101 print()102 print(''.center(os.get_terminal_size().columns, '-'))103 print()104def print_transmit(station_name, interval_routes, transmit_time_min, transmit_time_max):105 print('换乘方案'.center(os.get_terminal_size().columns-4, '-'))106 deep = len(interval_routes) - 1107 print('换乘方案 ', end='')108 for i in range(len(station_name)):109 if i != len(station_name) - 1:110 print('{} -> '.format(station_name[i]), end='')111 else:112 print('{} :\n'.format(station_name[i]))113 transmit_routes = []114 for r in interval_routes[0]:115 passed = False116 transmit_route, passed = search_transmit(interval_routes, r, deep, 0, passed, transmit_time_min, transmit_time_max)117 if passed:118 transmit_routes.append(transmit_route)119 print_recursion(transmit_routes, deep, 0)120 print(''.center(os.get_terminal_size().columns, '-'))121def search_transmit(interval_routes, route, deep, level, passed_old, transmit_time_min, transmit_time_max):122 next_route = interval_routes[level + 1]123 transmit_route = transmit()124 transmit_route.set_start(route)125 for nt in next_route:126 p = (datetime.strptime(nt.start_t, '%H:%M') - datetime.strptime(route.end_t, '%H:%M')).seconds / 60127 if p > transmit_time_min and p < transmit_time_max:128 if level + 1 != deep:129 passed = False130 nr, passed = search_transmit(interval_routes, nt, deep, level + 1, passed, transmit_time_min, transmit_time_max)131 if passed:132 transmit_route.add_end(nr)133 passed_old = passed_old if passed_old else passed134 else:135 transmit_route.add_end(nt)136 passed_old = True137 return transmit_route, passed_old138def print_recursion(routes, deep, level):139 if len(routes) == 0:140 print('找不到符合要求的车次')141 return142 for t in routes:143 for i in range(level):144 print(' ', end='')145 if level != deep:146 if level == 0:147 print()148 print('车次: {:5}, 时间: {}-{}, 历时: {}'.format(t.start.number, t.start.start_t, t.start.end_t, t.start.period), end='')149 for i in range(10):150 print(', {}: {}'.format(key_list[i], t.start.seats[key_list[i]]), end='')151 print()152 print_recursion(t.end, deep, level + 1)153 else:154 print('车次: {:5}, 时间: {}-{}, 历时: {}'.format(t.number, t.start_t, t.end_t, t.period), end='')155 for i in range(10):156 print(', {}: {}'.format(key_list[i], t.seats[key_list[i]]), end='')157 print('')158def start_chrome(port):159 os.system('chrome --remote-debugging-port='+str(port))160if __name__ == '__main__':161 now = datetime.now()162 cur_date = '-'.join([str(now.year), "{:0>2d}".format(now.month), "{:0>2d}".format(now.day)])163 parser = argparse.ArgumentParser(description='示例: python 12306.py --min-transmit-period 30 --max-transmit-period 90 --stations 深圳北 广州南 长沙南 武汉 郑州东 石家庄 北京西 --time 2022-01-22')164 parser.add_argument('--min-transmit-period', type=int, default=20, help='最小中转间隔时间(分钟)')165 parser.add_argument('--max-transmit-period', type=int, default=60, help='最大中转间隔时间(分钟)')166 parser.add_argument('--stations', type=str, nargs='+', help='中转换乘站点')167 parser.add_argument('--time', type=str, default=cur_date, help='乘车日期(YYYY-MM-DD)')168 parser.add_argument('--port', type=int, default=9221, help='远程打开chrome浏览器的端口')169 args = parser.parse_args()170 transmit_time_min = args.min_transmit_period171 transmit_time_max = args.max_transmit_period172 station_name = args.stations173 time = args.time174 port = args.port175 _thread.start_new_thread(start_chrome, (port,))176 print('等待浏览器启动')177 sleep(5)178 options = Options()179 options.add_experimental_option("debuggerAddress", "127.0.0.1:"+str(port))180 driver = webdriver.Chrome(options=options)181 driver.get('https://www.12306.cn/index/')182 routes = []183 terminal_width = os.get_terminal_size().columns-4184 f = open('station_name.txt', encoding='utf-8')185 stationName = f.read()186 stations = stationName.split('@')187 del stations[0]188 station_dict = {}189 for station in stations:190 station_dict[station.split('|')[1]] = station.split('|')[2]191 print()192 print('中转参数'.center(os.get_terminal_size().columns-4, '-'))193 print('乘车日期: {}'.format(time))194 print('中转站点: {}'.format(' -> '.join(station_name)))195 print('最小中转间隔: {} 分钟'.format(transmit_time_min))196 print('最大中转间隔: {} 分钟'.format(transmit_time_max))197 print(''.center(os.get_terminal_size().columns, '-'))198 print()199 print('查询路线'.center(os.get_terminal_size().columns-4, '-'))200 for index in enumerate(station_name):201 route = []202 if index[0] + 1 == len(station_name):203 break204 start = index[1]205 end = station_name[index[0] + 1]206 try:207 print('从 {},{} 到 {},{}'.format(start, station_dict[start], end, station_dict[end]))208 except KeyError:209 print('找不到车站代码')210 211 input_start_end(driver, start, end, time)212 switch_to_newest_window(driver)213 get_trains(driver, start, end, route)214 routes.append(route)215 switch_to_newest_window(driver)216 print(''.center(os.get_terminal_size().columns, '-'))217 print()218 print_all_tickets(station_name, routes)219 print_transmit(station_name, routes, transmit_time_min, transmit_time_max)...

Full Screen

Full Screen

test_page_objects.py

Source:test_page_objects.py Github

copy

Full Screen

...11 def click_seleniumbase_io_link(self, sb):12 link = '#readme article a[href*="seleniumbase.io"]'13 sb.wait_for_element_visible(link)14 sb.js_click(link)15 sb.switch_to_newest_window()16class SeleniumBaseIOPage:17 def do_search_and_click(self, sb, search_term):18 if sb.is_element_visible('[for="__search"] svg'):19 sb.click('[for="__search"] svg')20 sb.type('form[name="search"] input', search_term)21 sb.click("li.md-search-result__item h1:contains(%s)" % search_term)22class MyTests(BaseCase):23 def test_page_objects(self):24 search_term = "SeleniumBase GitHub"25 expected_text = "seleniumbase/SeleniumBase"26 GooglePage().go_to_google(self)27 GooglePage().do_search(self, search_term)28 self.assert_text(expected_text, "#search")29 GooglePage().click_search_result(self, expected_text)...

Full Screen

Full Screen

testNewUser.py

Source:testNewUser.py Github

copy

Full Screen

...13 landing_page.start_new_app(data)14 email_page = landing_page.navigate_to('Email')15 email_page.sign_in_to_gmail(data)16 email_page.check_for_and_click_email_verification_link(data)17 email_page.switch_to_newest_window()18 ...

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 SeleniumBase 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