Farklı siteler için selenium ile bot yazarken kullandığım bazı kullanışlı yöntemler hakkında aldığım notlar:

Bu yazi taslak durumundadir / This article is in draft state.
Table of Contents

    Libraries

    Selenium

    apt install python3-pip
    pip3 install selenium==4.4.3    # It is used to avoid being affected by version differences (You can set latest version)

    Google Chrome

    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    dpkg -i google-chrome-stable_current_amd64.deb

    webdriver_manager

    Since it is difficult to download and update manuel drivers, you can use webdriver_manager which does this automatically.

    pip3 install webdriver-manager

    Example usage:

    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    
    chrome_options = Options()
    chrome_options.headless = False
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument(f'user-agent={"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"}')
    
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

    Telegram send

    Stok veya fiyat takibi yaparken anlik bildirim almak icin telegram botu kullanabilirsiniz. telegram-send kutuphanesini kullanmadan once bot father ile nasil kendi botunuzu olusturacaginizi kisa bir arastirmayla ogrenebilirsiniz. You can use telegram bot to get newly notification when stock and price tracking. Before use to telegram_send library you can learn with shortly searching that how to use bot father to create your own bot.

    An example telegram notification that sends a successful purchase as a screenshot:

    # https://teknotower.com/10-dakikada-telegram-botu-nasil-olusturulur/
    pip3 install telegram-send
    # Default user configure
    telegram-send --configure
    # Multiple user configure
    telegram-send --config user1.conf --configure
    telegram-send --config user2.conf --configure

    Example usage:

    import telegram_send
    
    message_1 = '🟢 [Notification] 🖥️: %s / %s / %s' % ('var1', 'var2', 'var3')
    message_2 = '🟢 [Notification] 🖥️: %s / %s / %s' % ('var1', 'var2', 'var3')
    # Default user
    telegram_send.send(messages=[message_1])
    # Multiple user
    telegram_send.send(messages=[message_1], conf = '/home/user/user1.conf')
    telegram_send.send(messages=[message_2], conf = '/home/user/user2.conf')

    Amazon Captcha

    pip3 install amazoncaptcha

    Example usage:

    from amazoncaptcha import AmazonCaptcha
    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    
    chrome_options = Options()
    chrome_options.headless = False
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument(f'user-agent={"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"}')
    
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
    driver.get('https://www.amazon.com/errors/validateCaptcha')
    
    def take_screenshot(name):
        S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
        driver.set_window_size(S('Width'),S('Height'))
        driver.find_element_by_tag_name('body').screenshot(f'/root/projects/amazon/{name}')
    
    captcha = AmazonCaptcha.fromdriver(driver)
    take_screenshot("captcha_test.png")
    solution = captcha.solve()
    print(solution)
    
    driver.close()

    Test Selenium

    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    
    chrome_options = Options()
    chrome_options.headless = False
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument(f'user-agent={"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"}')
    
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
    driver.get('https://fsf.org')
    print(driver.title)
    driver.quit()

    Login with Cookie FIle

    When the page opened login manually and save the cookies to cookies.pkl file with pickle library. Use cookies.pkl file in your script for login.

    The pickle module is not secure. Only unpickle data you trust.

    Get Cookies Script

    import pickle
    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.support.ui import WebDriverWait
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    
    BASE_URL = "https://example.com"
    PROJECT_DIR = "/home/user/projects/name_bot"
    
    DRIVER_DELAY = 5    # Maximum delay
    
    chrome_options = Options()
    chrome_options.headless = False
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument(f'user-agent={"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"}')    # Mobile browser emulate
    
    class example:
        def __init__(self):
            self.loop = True
            self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
    
        def sign_in(self):
            """ Sign into site with product. """
    
            self.driver.get(BASE_URL)
    
            # User Agent
            print("<User Agent>")
            print(self.driver.execute_script("return navigator.userAgent;"))
    
            # Wait for manuel login
            time.sleep(30)
    
            # Save cookies
            pickle.dump(self.driver.get_cookies() , open("cookies.pkl","wb"))
    
            if self.action_delay('//div[contains(@class, "attend_appointment")]'):
                print("Login Success")
    
        def action_delay(self, xpath):
            try:
                WebDriverWait(self.driver, DRIVER_DELAY).until(EC.presence_of_element_located((By.XPATH, xpath)))
                return True
            except TimeoutException:
                return False
    
        def close_browser(self):
            """ Closes driver. """
            self.driver.close()
    
    
    if __name__ == '__main__':
        try:
            example_bot = example()
            example_bot.sign_in()
        except KeyboardInterrupt:
            example_bot.close_browser()

    Usege of Cookie File

    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    
    chrome_options = Options()
    chrome_options.headless = False
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument(f'user-agent={"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"}')
    
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
    self.driver.get('example.com')
    time.sleep(5)
    
    # Login with cookie file
    cookies = pickle.load(open(PROJECT_DIR + "/cookies.pkl", "rb"))
    for cookie in cookies:
        self.driver.add_cookie(cookie)
    
    # Reflesh page
    self.driver.refresh()

    Random User Agent

    import random
    
    class UserAgent:
        agent = {}
    
        def random(self):
            self.get_platform()
            self.get_os()
            self.get_browser()
    
            if self.agent['browser'] == 'Chrome':
                webkit = str(random.randint(500, 599))
                version = "%s.0%s.%s"%(str(random.randint(0, 24)), str(random.randint(0, 1500)), str(random.randint(0, 999)))
    
                return "Mozilla/5.0 (%s) AppleWebKit/%s.0 (KHTML, live Gecko) Chrome/%s Safari/%s"%(self.agent['os'], webkit, version, webkit)
            elif self.agent['browser'] == 'Firefox':
                year = str(random.randint(2000, 2015))
                month = str(random.randint(1, 12)).zfill(2)
                day = str(random.randint(1, 28)).zfill(2)
                gecko = "%s%s%s"%(year, month, day)
                version = "%s.0"%(str(random.randint(1, 15)))
    
                return "Mozillia/5.0 (%s; rv:%s) Gecko/%s Firefox/%s"%(self.agent['os'], version, gecko, version)
            elif self.agent['browser'] == 'IE':
                version = "%s.0"%(str(random.randint(1, 10)))
                engine = "%s.0"%(str(random.randint(1, 5)))
                option = random.choice([True, False])
                if option:
                    token = "%s;"%(random.choice(['.NET CLR', 'SV1', 'Tablet PC', 'Win64; IA64', 'Win64; x64', 'WOW64']))
                else:
                    token = ''
    
                return "Mozilla/5.0 (compatible; MSIE %s; %s; %sTrident/%s)"%(version, self.agent['os'], token, engine)
    
        def get_os(self):
            if self.agent['platform'] == 'Machintosh':
                self.agent['os'] = random.choice(['68K', 'PPC'])
            elif self.agent['platform'] == 'Windows':
                self.agent['os'] = random.choice(['Win3.11', 'WinNT3.51', 'WinNT4.0', 'Windows NT 5.0', 'Windows NT 5.1', 'Windows NT 5.2', 'Windows NT 6.0', 'Windows NT 6.1', 'Windows NT 6.2', 'Win95', 'Win98', 'Win 9x 4.90', 'WindowsCE'])
            elif self.agent['platform'] == 'X11':
                self.agent['os'] = random.choice(['Linux i686', 'Linux x86_64'])
    
        def get_browser(self):
            self.agent['browser'] = random.choice(['Chrome', 'Firefox', 'IE'])
    
        def get_platform(self):
            self.agent['platform'] = random.choice(['Machintosh', 'Windows', 'X11'])
    0 - 0

    Thank You For Your Vote!

    Sorry You have Already Voted!