Home > Software engineering >  Twitter bot with selenium
Twitter bot with selenium

Time:11-20

I want to make Twitter bot with selenium but the Chrome window closes as soon as it opens. How can I fix this?

My Code:

from userinfo import username, password 
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

class twitter:
    def __init__(self,username,password):
        self.browserProfile=webdriver.ChromeOptions()
        self.browserProfile.add_experimental_option('prefs',{'intl.accept_languages':'en,en_US'})
        self.browser=webdriver.Chrome('chromedriver.exe',chrome_options=self.browserProfile)
        self.username=username
        self.password=password
    
    def sıgnIn(self):
        self.browser.get("https://twitter.com/i/flow/login")
        
        time.sleep(2)
        self.browser.maximize_window()
        search=self.browser.find_element(By.XPATH,"//*[@id='react-root']/div/div/div/main/div/div/div/div[2]/div[2]/div/div[5]/label/div/div[2]/div/input")
        search.click()
        search.send_keys(self.username)
        search.send_keys(Keys.ENTER)
        sifre=self.browser.find_element(By.XPATH,"//*[@id='react-root']/div/div/div/main/div/div/div/div[2]/div[2]/div[1]/div/div/div/div[3]/div/label/div/div[2]/div[1]/input")
        sifre.click()
        time.sleep(2)
        sifre.send_keys(self.password)
        sifre.send_keys(Keys.ENTER)
        
giris=twitter(username,password)
giris.sıgnIn()
        

CodePudding user response:

Add the below chrome options in __init__() function:

self.browserProfile.add_experimental_option("detach", True)
  • Related