Skip to main content
 首页 » 编程设计

python Selenium : Get dynamic update of text of an element

2024年11月24日8myhome

我在 Python 和 Selenium 框架的一个基本问题上失败了。我只想循环读取元素的文本。到目前为止,我已经可以引用元素并阅读文本了。 但是,当我在无限循环中访问 .text 属性时,短时间后出现以下错误:

Traceback (most recent call last): 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connection.py", line 159, in _new_conn 
    (self._dns_host, self.port), self.timeout, **extra_kw) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection 
    raise err 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection 
    sock.connect(sa) 
OSError: [WinError 10048] Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden 
 
During handling of the above exception, another exception occurred: 
 
Traceback (most recent call last): 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen 
    chunked=chunked) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 354, in _make_request 
    conn.request(method, url, **httplib_request_kw) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\Lib\http\client.py", line 1107, in request 
    self._send_request(method, url, body, headers) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\Lib\http\client.py", line 1152, in _send_request 
    self.endheaders(body) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\Lib\http\client.py", line 1103, in endheaders 
    self._send_output(message_body) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\Lib\http\client.py", line 934, in _send_output 
    self.send(msg) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\Lib\http\client.py", line 877, in send 
    self.connect() 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connection.py", line 181, in connect 
    conn = self._new_conn() 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connection.py", line 168, in _new_conn 
    self, "Failed to establish a new connection: %s" % e) 
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001D2BB3B8710>: Failed to establish a new connection: [WinError 10048] Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden 
 
During handling of the above exception, another exception occurred: 
 
Traceback (most recent call last): 
  File "C:/Users/lucci/Documents/Casino/03 Roulette/Python_Doup_Detection/main.py", line 54, in <module> 
    print(text_status.text) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 76, in text 
    return self._execute(Command.GET_ELEMENT_TEXT)['value'] 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute 
    return self._parent.execute(command, params) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute 
    response = self.command_executor.execute(driver_command, params) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute 
    return self._request(command_info[0], url, body=data) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 402, in _request 
    resp = http.request(method, url, body=body, headers=headers) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\request.py", line 68, in request 
    **urlopen_kw) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\request.py", line 89, in request_encode_url 
    return self.urlopen(method, url, **extra_kw) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\poolmanager.py", line 323, in urlopen 
    response = conn.urlopen(method, u.request_uri, **kw) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 667, in urlopen 
    **response_kw) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 667, in urlopen 
    **response_kw) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 667, in urlopen 
    **response_kw) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen 
    _stacktrace=sys.exc_info()[2]) 
  File "C:\Users\lucci\AppData\Local\Programs\Python\Python35\lib\site-packages\urllib3\util\retry.py", line 398, in increment 
    raise MaxRetryError(_pool, url, error or ResponseError(cause)) 
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=63440): Max retries exceeded with url: /session/A78CB8DA-2682-455E-AB7B-F92BEF4922EA/element/6665b99e-a950-49bd-bc19-e4c5db372d6a/text (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001D2BB3B8710>: Failed to establish a new connection: [WinError 10048] Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden',)) 
 

错误消息指出每个套接字地址只能使用一次。产生上述错误的代码如下:

 
text_status = WebDriverWait(driver, TIMEOUT).until(EC.presence_of_element_located((By.CLASS_NAME, "text--34J_5 uppercase--ii2bO font-small--19CMC"))) 
if text_status is not None: 
    print("Found status") 
 
while True: 
    print(text_status.text) 

如果您能提供帮助,我将不胜感激。我只想实时访问元素的文本。文本可以相对快速地改变(大约每秒 2 次)。谁能告诉我该怎么做或我的错误在哪里?

编辑: 我想我的问题与这个问题有关:Python SkypeWebClient-Bot with Selenium (Error 10048)但是这个也没有答案。