![](/img/trans.png)
[英]Python - No connection could be made because the target machine actively refused it
[英]No connection could be made because the target machine actively refused it Python
所以我一直卡在這里無法轉發go大約2周。 我試圖找到一個元素並截取它的屏幕截圖,但我一直收到這個
No connection could be made because the target machine actively refused it
這是我要獲取的元素:
<img id="action_captcha" src="URL HERE" alt="CAPTCHA code" style="vertical-align:middle;">
這是我的代碼:
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)
我得到的只是
1
No connection could be made because the target machine actively refused it
我知道沒有防火牆阻止連接,因為我在同一個瀏覽器實例中得到了很多元素。 我不知道是什么問題。 任何幫助表示贊賞。 謝謝。
“主動拒絕”意味着當您嘗試連接時,主機發送了重置而不是確認。 因此,這在您的代碼中不是問題。 要么是防火牆阻止了連接,要么是托管服務的進程沒有在那個端口上偵聽,這可能是因為它根本沒有運行或者因為它在不同的端口上偵聽。
您可以在包含瀏覽器元素的頁面中導航嗎? 我毫無問題地使用了相同的代碼,並且成功地從工作站點制作了驗證碼的屏幕截圖。
在另一個頁面中嘗試一個示例,以確保所有內容都被阻止從您嘗試訪問的頁面。
例如:
driver.get("https://captchas.net/")
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "captcha_pic"))
)
image = element.screenshot("Captcha.png")
我不願意承認我已經在這個問題上停留了 2 周,在發布問題 3 小時后,我解決了它。
我收到此錯誤的原因是因為我在 function 中初始化了驅動程序,所以我不得不將其作為參數傳遞。 這就是我的意思:
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)
所以在這里我必須像這樣初始化 X
def x(browser):
......
然后這樣稱呼它
x(driver)
NGL 對我來說有點愚蠢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.