简体   繁体   中英

socket.error: [Errno 54] Connection reset by peer Selenium-python

I am new to selenium and trying an exampler http://www.marinamele.com/selenium-tutorial-web-scraping-with-selenium-and-python">here

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


def init_driver():
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 5)
    return driver


def lookup(driver, query):
    driver.get("http://www.google.com")
    try:
        box = driver.wait.until(EC.presence_of_element_located(
            (By.NAME, "q")))
        button = driver.wait.until(EC.element_to_be_clickable(
            (By.NAME, "btnK")))
        box.send_keys(query)
        button.click()
    except TimeoutException:
        print("Box or Button not found in google.com")


if __name__ == "__main__":
    driver = init_driver()
    lookup(driver, "Selenium")
    time.sleep(5)
    driver.quit()

I have installed selenium using

pip install selenium

But Its not working.

Error showing up is

socket.error: [Errno 54] Connection reset by peer

and sometimes

raise BadStatusLine(line)
httplib.BadStatusLine: ''

Firefox open up and then shuts down saying "Firefox has quit unexpectedly"

I have gone through Selenium headless browser webdriver [Errno 104] Connection reset by peer and Why am I getting this error in python ? (httplib) but nothing helped.

Downgrade your firefox and try again.Looks like all versions of firefox not compatible with selenium.

Reference: Selenium 2.50 not working on firefox 45

It sounds obvious, but in opposition to the existing answer I want to add a reminder to try upgrading your Selenium, Firefox & geckodriver instances.

I spent a lot of time trying to solve this issue before I remembered to try a simple update, which resolved the problem.

--

Update Selenium in Python with

pip install --upgrade selenium

Check geckodriver version

geckodriver --version

Install latest gecko driver and Firefox

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