3

I am failing on a fundamental problem with Python and the Selenium framework. All I want to do is read the text of an element cyclically. I have come so far that I can reference the element and read the text. But when I access the .text property in an infinite loop, I get the following error after a short time:

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',))

The error message states that each socket address may only be used once. The code that produces the said error is the following:


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)

I would be very grateful for help. I just want to access the element's text in real time. The text can change relatively quickly (about 2 times a second). Can anyone tell me how to do this or where my mistake lies?

EDIT: I think my question is related to this one: Python SkypeWebClient-Bot with Selenium (Error 10048) But this one also has no answer.

Lukas Nothhelfer
  • 820
  • 1
  • 7
  • 23
  • selenium update the text automatically but you can do `text_status.refresh()` but this send a new request to the http server it like the refresh in your browser – dsal3389 Mar 25 '19 at 22:07
  • @dsal3389 Thank you for your response. I am able to get the text from `text_status` but the problem is that after some loops i get the posted error. As you can see I am gaining a reference to the element only once. In the loop I extract the current text with `text_status.text`. But this crashes after some loops. – Lukas Nothhelfer Mar 25 '19 at 22:11

2 Answers2

0

You can potentially run into stale element exceptions in addition to what you are already seeing. I suggest that you find the element each time you want to read the text. This will ensure that you have an active element and should not run into the above exception.

while True:

    text_status = WebDriverWait(driver,TIMEOUT).until(EC.presence_of_element_located((By.CLASS_NAME, "text--34J_5 uppercase--ii2bO font-small--19CMC")))

    print(text_status.text)
RKelley
  • 1,099
  • 8
  • 14
  • That is possible. But that is not a problem at the moment. Unfortunately, your answer does not help me to solve my problem. Thanks anyway for your efforts. – Lukas Nothhelfer Mar 25 '19 at 22:51
0

I could solve my problem myself. It seems there are problems with MicrosoftWebDriver when using MS Edge. Apparently, this does not support the keepalive flag, so a new connection will be opened for each action. If you take many actions, sooner or later this will lead to an overload and thus a crash. That's why I downloaded geckodriver for Mozilla Firefox and now it works. Thanks to everyone who has tried to help me.

Lukas Nothhelfer
  • 820
  • 1
  • 7
  • 23