'selenium twitter login without new device notification

Is there a way to log in to twitter with selenium/python without getting the notification "login from a new device" on Twitter?

Current code is

import urllib.request
import selenium
from bs4 import BeautifulSoup
import requests 
import re
import json

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path='./chromedriver')
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
print(driver.execute_script("return navigator.userAgent;"))

def twitter_login(driver, username, password):
    driver.get('https://twitter.com/login')
    driver.find_element_by_xpath("//input[contains(@name,'username')]").send_keys(username)
    driver.find_element_by_xpath("//input[contains(@name,'password')]").send_keys(password)
    driver.find_elements_by_xpath("//*[contains(text(), 'Log in')]")[1].click()


twitter_login(driver, username, password)


Solution 1:[1]

You can start Selenium with a Chrome profile that is already familiar with your Twitter account. That way, you won't be bothered by any notifications.

options.add_argument(r"--user-data-dir=C:\Users\Me\AppData\Local\Google\Chrome\User Data") # Enter 'chrome://version' in the adress bar and paste in the Profile Path 
options.add_argument(r"--profile-directory=Profile 4") # The profile name can be found in the last bit of the Profile Path 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 oerol