简体   繁体   中英

No connection could be made because the target machine actively refused it Python

So I have been stuck here unable to go forward for about 2 weeks. I am trying to find an element and take a screenshot of it, but I keep getting this

No connection could be made because the target machine actively refused it

here is the element i am trying to get:

<img id="action_captcha" src="URL HERE" alt="CAPTCHA code" style="vertical-align:middle;">

and this is my code:

try:
    print("1")
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "action_captcha"))
    )
    print("2")
    image = element.screenshot("Captcha.png")
    print("3")
except Exception as e:
    print(e)

all i get is

1
No connection could be made because the target machine actively refused it

I know there is no firewall blocking the connection since i got a lot of elements before this in the same browser instance. I don't know what is the problem. Any help is appreciated. Thanks.

"Actively refused it" means that the host sent a reset instead of an ack when you tried to connect. It is therefore not a problem in your code. Either there is a firewall blocking the connection or the process that is hosting the service is not listening on that port, this may be because it is not running at all or because it is listening on a different port.

Can you navigate in that page that contains the element from your browser? I used the same code without problem, and I made a screenshot of a Captcha from a working site successfully.

Try an example in another page to make sure that everything is blocked from the page you are trying to reach.

Eg:

driver.get("https://captchas.net/")
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "captcha_pic"))
)
image = element.screenshot("Captcha.png")

I hate to admit that i have been stuck on this problem for 2 weeks, and 3 hours after posting a question, I solve it.

The reason I was getting this error was because I was initialized the driver inside of a function so I had to pass it as a parameter. Here is what I mean:

def f():
    try:
        #Initaiates the chrome browser
        driver = webdriver.Chrome(path, options=options)
        driver.get(URL)
        x()

    #Catches any errors that might occur
    except Exception as e:
        print(e)
        print("Error")
        driver.quit()

def x():
    try:
        element = WebDriverWait(browser, 10).until(
            EC.presence_of_element_located((By.ID, "action_captcha"))
        )
        
        image = element.screenshot("C:\\Users\\moham\\Desktop\\TLS\\Webscraping\\TLS Appointment\\Captchas\\Original Captcha.png")
    except Exception as e:
        print(e)

so here I have to initialize X like this

def x(browser):
    ......

and then call it like this

x(driver)

Kinda stupid on my part NGL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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