How to automate a new identity in Tor with Selenium & Python on Windows?

After trying so many solutions, I finally managed to open the Tor browser, by this on my Windows machine:

from the selenium import webdriver
since selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary ("Browser C:  Program Files  Tor  Browser  firefox.exe")
profile = webdriver.FirefoxProfile ("C:  Program Browser  Tor Browser  Browser  TorBrowser  Data  Browser  profile.default")
profile.set_preference (& # 39; network.proxy.type & # 39; 1)
profile.set_preference (& # 39; network.proxy.socks & # 39 ;, & # 39; 127.0.0.1 & # 39;)
profile.set_preference (& # 39; network.proxy.socks_port & # 39; 9050)

driver = webdriver.Firefox (executable_path = r "geckodriver.exe", firefox_binary = binary)
driver.get ("http://httpbin.org/ip")

although this is still not fully automated, because I have to manually start the Tor browser for it to work.

Now the challenge is to automatically generate a new identity in Tor because I managed to get another solution that works for me, like this:

import requests
import signal
from the stem.control import controller

def get_tor_session ():
session = requests.session ()
session.proxies = {http: socks5: //127.0.0.1: 9050, https: socks5: //127.0. 0.1: 9050 & # 39;
return session

def renew_connection ():
with Controller.from_port (port = 9051) as a controller:
controller.authenticate (password = "tor12345")
controller.signal (Signal.NEWNYM)

session = get_tor_session ()
print ("Tor IP:", session.get ("http://httpbin.org/ip") .text)
print ("Primary Ip:", requests.get ("http://httpbin.org/ip") .text)

renew_connection ()
session = get_tor_session ()
print ("Tor New Ip:", session.get ("http://httpbin.org/ip") .text)

here it uses Tor Bundle and must also manually run the tor.exe file from the tor bundle folder, then it works.

I want to generate a new identity using selenium because I want to scrape dynamic web pages, which have javascripts, mouse buttons, and so on.

Suggestions are greatly appreciated!