I am writing a script that will check if the proxy is working. The program should: 1. Load the proxy from the list (txt). 2. Go to any page (for example wikipedia) 3. If the page has loaded (even not completely) it saves the proxy data to another txt file.
It must all be in the loop. It must also check whether the browser has displayed an error. I have a problem with always turning off the previous browser every time, after several loops several browsers are already open.
Ps. I replaced the iteration with a random number
from selenium import webdriver
import random
from configparser import ConfigParser
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import traceback
while 1:
    ini = ConfigParser()
    ini.read('liczba_proxy.ini')
    random.random()
    liczba_losowa = random.randint(1, 999)
    f = open('user-agents.txt')
    lines = f.readlines()
    user_agent = lines[liczba_losowa]
    user_agent = str(user_agent)
    s = open('proxy_list.txt')
    proxy = s.readlines()
    i = ini.getint('liczba', 'liczba')
    prefs = {"profile.managed_default_content_settings.images": 2}
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--proxy-server=%s' % proxy[liczba_losowa])
    chrome_options.add_argument(f'user-agent={user_agent}')
    chrome_options.add_experimental_option("prefs", prefs)
    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:\Python\Driver\chromedriver.exe')
    driver.get('https://en.wikipedia.org/wiki/Replication_error_phenotype')
    def error_catching():
        print("error")
        driver.stop_client()
        traceback.print_stack()
        traceback.print_exc()
        return False
    def presence_of_element(driver, timeout=5):
        try:
            w = WebDriverWait(driver, timeout)
            w.until(EC.presence_of_element_located((By.ID, 'siteNotice')))
            print('work')
            driver.stop_client()
            return True
        except:
            print('not working')
            driver.stop_client()
            error_catching()
                Without commenting on your code design:
In order to close a driver instance, use driver.close() or driver.quit() instead of your driver.stop_client().
The first one closes the the browser window on which the focus is set. The second one basically closes all the browser windows and ends the WebDriver session gracefully.
Use
chrome_options.quit()
Obs.: Im pretty sure you should not use testcases like that... "while 1"? so you test will never end?
I guess you should setup your testes in TestCases and call the TheSuite to teste all your testcases and give you one feedback about whant pass or not, and maybe setup one cronjob to keep calling it by time to time.
Here one simple example mine using test cases with django and splinter (splinter is build on top of selenium)
https://github.com/Diegow3b/python-django-basictestcase/blob/master/myApp/tests/test_views.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With