It is necessary in a bunch of Python + Selenium to open a new tab in Chrome, but googled, and did not find information.

The works() function algorithm:

  1. If i==n , the program stops its work.
  2. If j==50 , the program opens a new window with the desired site.
  3. If these options do not work, the program opens a new tab in the last opened instance of the browser window with the desired site.

Code:

 #-*-coding:UTF-8-*- from selenium import webdriver from selenium.common.exceptions import NoSuchElementException import time import subprocess import sys import os i=0 def works(url, n): global i if i==n: os.startfile(r'5.py') sys.exit() elif j==50: chrome_options = webdriver.ChromeOptions() driver = webdriver.Chrome(executable_path=r"D:\selenium\chromedriver.exe", chrome_options=chrome_options) driver.get(url) driver.implicitly_wait(10) # seconds driver.get(url) time.sleep(3) print (n) print (i) # проверка на правильное выполнение цикла try: time.sleep(3) driver.find_element_by_id('videoContainer') except NoSuchElementException: print('Такой элемент на сайте не существует!') subprocess.getoutput("taskkill /f /im chrome.exe") subprocess.getoutput("taskkill /f /im chromedriver.exe") i = 0 j = 0 time.sleep(10) else: print('Такой элемент на сайте существует!') time.sleep(3) i = i + 1 j = j + 1 print (i) # проверка на правильное выполнение цикла finally: f = open('i.txt', 'w') s = str(i) f.write(s) f.close() f = open('monitor.txt', 'w') f.close() works(url, n) else: def main(): f = open('monitor.txt', 'w') f.close() url = input("Введи ссылку на трансляцию: ") n = int(input("Введи количество просмотров трансляции: ")) f = open('url.txt', 'w') f.write(url) f.close() f = open('n.txt', 'w') s = str(n) f.write(s) f.close() works(url, n) if __name__ == '__main__': main() 

    1 answer 1

    Times: https://stackoverflow.com/a/17558909/2787185

    Two: https://stackoverflow.com/a/28432939/2787185

    Main idea:

     body.send_keys(Keys.CONTROL + 't')