Automating different websites with both chrome and Firefox

idris_fagbemi_c31c05906e8

Idris Fagbemi

Posted on November 4, 2021

Automating different websites with both chrome and Firefox

β€œfrom selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from time import sleep

class Bot:
def init(self, browser, url, search):
self.browser = browser
self.url = url
self.search = search

    if self.browser == 'Chrome':
        self.driver = webdriver.Chrome(r'C:\chromedriver.exe')
    elif self.browser == 'Firefox':
        options = Options()
        options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
        self.driver = webdriver.Firefox(options=options, executable_path=r'C:\geckodriver.exe')

def open_browser(self):
    if self.browser == 'Chrome':
        self.driver.get(self.url)
    elif self.browser == 'Firefox':
        self.driver.get(self.url)
    else:
        print('Browser not found')

def search_string(self):
    if 'geeksforgeeks' in self.url:

        self.driver.find_element(By.CSS_SELECTOR, '.gfg-icon_search').click()
        search_field = self.driver.find_element(By.ID, 'gcse-search-input')
        search_field.send_keys(self.search)
        search_field.send_keys(Keys.RETURN)
        sleep(5)
    elif 'amazon' in self.url:
        search_field = self.driver.find_element(By.ID, 'twotabsearchtextbox')
        search_field.send_keys(self.search)
        search_field.send_keys(Keys.RETURN)
        sleep(5)
    else:
        print('Search string not found')

def close_browser(self):
    if self.browser == 'Chrome':
        self.driver.close()
    elif self.browser == 'Firefox':
        self.driver.close()
    else:
        print('Browser not found')
Enter fullscreen mode Exit fullscreen mode

class AmazonShopping(Bot):
def init(self, browser, url, search, item_to_add):
super().init(browser, url, search)
self.item_to_add = item_to_add

def open_browser(self):
    super().open_browser()
    self.product_name = ""
    self.rent = ""
    self.products = ""
    self.items = []

def click_product(self):
    self.products = self.driver.find_elements(By.CLASS_NAME, 's-image')
    for prod in self.products:
        self.items.append(prod.get_attribute('alt'))

    for item in self.items:
        self.driver.implicitly_wait(10)
        if self.item_to_add in item:
            self.driver.find_element(By.LINK_TEXT, item).click()
            sleep(10)
        else:

            pass

def add_to_cart(self):
    self.driver.find_element(By.CSS_SELECTOR, '#submit\.add-to-cart').click()
    sleep(10)”
Enter fullscreen mode Exit fullscreen mode
πŸ’– πŸ’ͺ πŸ™… 🚩
idris_fagbemi_c31c05906e8
Idris Fagbemi

Posted on November 4, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024