【问题标题】:BrokenPipeError while using selenium and chromedriver使用 selenium 和 chromedriver 时出现 BrokenPipeError
【发布时间】:2018-04-15 21:36:36
【问题描述】:

在执行以下代码时,尝试抓取网站的索引页面,并尝试识别包含登录文本的元素,如下所示:

from selenium import webdriver
import os
chromedriver = "/usr/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
my_url = 'https://www.pastest.com/'
driver.implicitly_wait(15)
driver.get(my_url)
driver.quit()
p_element = driver.find_element_by_class_name('popup-link')
print(p_element.text)

我收到以下错误:

Traceback (most recent call last):
  File "scrape_pastest.py", line 17, in <module>
    p_element = driver.find_element_by_class_name('popup-link')
  File "/home/joel/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 555, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "/home/joel/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
    'value': value})['value']
  File "/home/joel/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 310, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/joel/.local/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 466, in execute
    return self._request(command_info[0], url, body=data)
  File "/home/joel/.local/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 489, in _request
    self._conn.request(method, parsed_url.path, body, headers)
  File "/usr/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1065, in _send_output
    self.send(chunk)
  File "/usr/lib/python3.6/http/client.py", line 986, in send
    self.sock.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe

当我第一次遇到错误时,我专门添加了 driver.implicitly_wait(15) 行来处理任何超时错误。我也试过selenium_client.set_page_load_timeout(10),但也没有用。

这个问题是由什么引起的?我该如何解决?

【问题讨论】:

    标签: python selenium selenium-chromedriver


    【解决方案1】:

    quit 调用find_element_by_class_name 后的驱动程序。

    根据 selenium.webdriver docs:

    退出()

    Closes the browser and shuts down the ChromeDriver executable that is started when starting the ChromeDriver
    

    driver.quit 关闭浏览器会话,之后您将无法使用驱动程序搜索元素。

    【讨论】:

      最近更新 更多