Reputation: 13
hello i have written the below code to visit a website throw a proxy and click a button and then close the browser and repeat the about sets however with a different proxy. however the browser is not closing so there is a build up browsers. here is the code:
from selenium import webdriver
import time
import os, re
import psutil
import signal
print("*" * 60)
print("LOL soul clicker")
print("*" * 60)
with open("working.txt", "r" ,encoding ="utf-8") as data:
text=data.readlines()
data.close()
browser = webdriver.Firefox()
def workclick(proxy, proxy_port):
target_website = "https:www.website.com"
proxy_profile = webdriver.FirefoxProfile()
proxy_profile.set_preference("network.proxy.type", 1)
proxy_profile.set_preference("network.proxy.http", proxy )
proxy_profile.set_preference("network.proxy.http_port", proxy_port)
proxy_profile.set_preference("network.proxy.ssl", proxy )
proxy_profile.set_preference("network.proxy.ssl_port", proxy_port)
browser = webdriver.Firefox(firefox_profile=proxy_profile)
target_website = browser.get(target_website)
time.sleep(6)
target_website.find_element_by_xpath('/html/body/div[7]/div[2] /div[1]').click()
target_website.find_element_by_xpath('/html/body/div[5]/div[2]/div[1]').click()
target_website.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/div[1]/div/div[1]/article/div[1]/div/div[2]/a/i').click()
browser.close()
def getpid():
x = ""
pn = "geckodriver.exe"
for proc in psutil.process_iter():
if proc.name() == pn:
x=int(str(proc).split(",")[0].split("=")[1])
try:
os.kill(x, signal.SIGTERM)
except:
os.kill(x, signal.SIGTERM)
k = len(text)
print(k)
for i in text:
try:
proxy, proxy_port = i.split(":")
proxy = str(proxy)
proxy_port = int(proxy_port)
workclick(proxy, proxy_port)
time.sleep(60)
getpid()
print(f"Success : {proxy} {k}")
k=k-1
except:
print(f"Failed : {proxy} {k}")
k=k-1
getpid()
so i have trying simply debugging the code piece by piece in idle but i can not for the love of me seem to get the code to work correctly. basically i want to be able to click a button through a proxy several times
so i would greatly appreciate as much help as possible to get this script up and running to the best and most efficient way as possible thank you all.
Upvotes: 1
Views: 259
Reputation: 1184
My first advices,
Firstly, Don't use sleep() , use wait until methods. It makes your tests more stable because sleep(6) is waiting 6 seconds every time and it can cause timing issues and also if elements does not appear on this time your code will fail. (https://selenium-python.readthedocs.io/waits.html)
After that you can use browser.quit() instead of browser.close().
Thirdly, If you will improve this code, try to work with your own libraries. You can write a custom methods which include what you wants, Then import them into your test files as a custom library.
For example: You can create SetProxy.py script and define proxies in this file as a custom function, then call them into test functions. It will be more maintainable when your code expands.
Upvotes: 1