Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用selenium和python使此代码更高效_Python_Selenium_Click_Signals_Psutil - Fatal编程技术网

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

如何使用selenium和python使此代码更高效,python,selenium,click,signals,psutil,Python,Selenium,Click,Signals,Psutil,你好,我写了下面的代码来访问一个网站,抛出一个代理,点击一个按钮,然后关闭浏览器,然后用另一个代理重复关于集合。但是,浏览器未关闭,因此存在浏览器的堆积。 代码如下: from selenium import webdriver import time import os, re import psutil import signal print("*" * 60) print("LOL soul clicker") print("*" * 60) with open("working.

你好,我写了下面的代码来访问一个网站,抛出一个代理,点击一个按钮,然后关闭浏览器,然后用另一个代理重复关于集合。但是,浏览器未关闭,因此存在浏览器的堆积。 代码如下:

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()
所以我试着在空闲的情况下一段一段地调试代码,但我不能因为爱我而让代码正常工作。基本上,我希望能够通过代理多次单击按钮

因此,我将非常感谢尽可能多的帮助,让这个脚本运行到最佳和最有效的方式尽可能谢谢大家

我的第一个建议

首先,不要使用sleep(),使用wait-until方法。它使您的测试更加稳定,因为睡眠(6)每次都要等待6秒,这可能会导致计时问题,而且如果此时元素没有出现,您的代码将失败。()

之后,可以使用browser.quit()而不是browser.close()

第三,如果您想改进此代码,请尝试使用您自己的库。您可以编写包含所需内容的自定义方法,然后将它们作为自定义库导入到测试文件中

例如: 您可以创建SetProxy.py脚本,并将此文件中的代理定义为自定义函数,然后将它们调用到测试函数中。当代码扩展时,它将更易于维护