简体   繁体   English

如何使用 selenium 和 python 使这段代码更高效

[英]how to make this code more efficient with selenium and python

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.你好,我已经编写了以下代码来访问一个网站,抛出一个代理并单击一个按钮,然后关闭浏览器并使用不同的代理重复 about 集。 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.因此,我将非常感谢尽可能多的帮助,以尽可能以最佳和最有效的方式启动并运行此脚本,谢谢大家。

My first advices,我的第一个建议,

Firstly, Don't use sleep() , use wait until methods.首先,不要使用 sleep() ,使用等待方法。 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.它使您的测试更加稳定,因为 sleep(6) 每次都等待 6 秒,这可能会导致计时问题,并且如果此时没有出现元素,您的代码将失败。 ( https://selenium-python.readthedocs.io/waits.html ) ( https://selenium-python.readthedocs.io/waits.html )

After that you can use browser.quit() instead of browser.close().之后,您可以使用 browser.quit() 而不是 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.例如:您可以创建 SetProxy.py 脚本并将此文件中的代理定义为自定义函数,然后将它们调用到测试函数中。 It will be more maintainable when your code expands.当您的代码扩展时,它将更易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM