R
R
r1mple2021-10-28 00:20:11
Python
r1mple, 2021-10-28 00:20:11

How to restart browser in selenium?

In general, I need to restart the browser in selenium, just restart. There is a for loop, and selenium is launched in it, at the next iteration I need to restart the browser, but with a different proxy, because the number of requests on the server is limited, I need to bypass this. Googled it - nothing works.
Here is the code

import sys
import config
from excel import Excel
import keyboard
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup

def cooldown(cooldown):
    for i in range(cooldown, 0, -1):
        print('Cooldown ends in {}'.format(i), end='')
        print('\r', end='')
        time.sleep(1)

for i in range(1, len(sys.argv)):
    if sys.argv[i] == '--help':
        print('1')
        sys.exit(0)

URL = sys.argv[1]
DEVICE = sys.argv[2] if sys.argv[2] == 'Desktop' or sys.argv[2] == 'desktop' or sys.argv[2] == 'mobile' or sys.argv[2] == 'Mobile' else sys.exit('Device is invalid. It\'s either "Desktop" or "Mobile"')

print('Url: {}\nDevice: {}'.format(URL, DEVICE))

keywords = []
resultArray = []

for array in Excel.getDataFromCSVToArray('keywords.csv'):
    for element in array:
        keywords.append(element)

keywordsSplit = [keywords[d:d+10] for d in range(0, len(keywords), 10)]

options = webdriver.ChromeOptions()

options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_argument('--ignore-gpu-blacklist')
options.add_argument('--use-gl')
options.add_argument('--disable-web-security')
options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0')
options.add_experimental_option("excludeSwitches", ['enable-logging'])
# options.add_extension(config.ADBLOCKPATH)

driver = webdriver.Chrome(chrome_options=options, executable_path=config.DRIVERPATH)
driver.get(config.APIURL)

#Wait till the page loads, even tho driver.get waits for the page to load, we still wait additional seconds for it
WebDriverWait(driver, 4).until(EC.presence_of_element_located((By.XPATH, '/html/body')))

#collecting the data, storing it in the resulting array
def collectData(keywordsIterable, iterable):
    inputField = driver.find_element_by_class_name('vs__search')
    inputField.click()
    inputField.clear()
    inputField.send_keys('Israel')
    inputField.send_keys(Keys.ENTER)
    selectDeviceButtons = driver.find_elements_by_class_name('btn-lg')
    selectDeviceButtons[0].click() if DEVICE.lower() == 'desktop' else selectDeviceButtons[1].click()

    domainField = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/section[2]/div/div/div[1]/div[3]/div/div[1]/input')
    domainField.clear()
    domainField.send_keys(URL)

    try:
        addKeywordsButton = driver.find_element_by_class_name('add-more-keywords')
        addKeywordsButton.click()
    except:
        pass

    try:
        for i in range(0, 10):
            driver.find_element_by_name(f'keyword{str(i+1)}').clear()
            driver.find_element_by_name(f'keyword{str(i+1)}').send_keys(keywordsIterable[iterable][i])
    except:
        pass

    print('Solve the captcha. \nOnce you are done press "Shift + g".')
    print('When results are loaded press "Shift + g".')
    keyboard.wait('shift + g') # wait tille the results loads

    results = BeautifulSoup(driver.find_element_by_xpath('//*[@id="appFreeSerpChecker"]/section[2]/div/div/div[3]/div').get_attribute('innerHTML'), 'html.parser')
    resultArray.append(results.find_all('div', class_='keyword-block'))
    
for i in range(0, len(keywordsSplit)):
    if i == len(keywordsSplit):
        collectData(keywordsSplit, i)
        driver.refresh()
        print('Done.')
    collectData(keywordsSplit, i)
    driver.refresh()
    print('Got all the data from this page.')
    cooldown(15)

driver.close()

print(resultArray)
# Analyze the data

input('Any key to exit the programm...')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question