执行以下代码以尝试抓取网站的索引页面,并尝试识别包含登录文本的元素,如下所示:
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)
,但这也没有帮助。
这个问题到底是什么原因?我该如何解决?
答案 0 :(得分:3)
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
关闭浏览器会话,之后您无法使用驱动程序来搜索元素。