Python Selenium:获取元素文本的动态更新

时间:2019-03-25 21:56:13

标签: python selenium text

我在Python和Selenium框架的一个基本问题上失败了。我要做的就是循环读取元素的文本。到目前为止,我可以引用该元素并阅读文本。 但是,当我在无限循环中访问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)

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

checked

我将非常感谢您的帮助。我只想实时访问元素的文本。文本可以相对快速地更改(每秒约2次)。谁能告诉我该怎么做或我的错误所在?

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

2 个答案:

答案 0 :(得分:0)

除了已经看到的内容之外,您还可能会遇到过时的元素异常。我建议您每次想阅读文本时都要查找元素。这将确保您具有活动的元素,并且不会遇到上述异常。

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)

答案 1 :(得分:0)

我可以自己解决问题。使用MS Edge时,MicrosoftWebDriver似乎有问题。显然,这不支持keepalive标志,因此将为每个操作打开一个新的连接。如果您执行许多操作,迟早会导致过载,从而导致崩溃。这就是为什么我为Mozilla Firefox下载geckodriver并现在可以使用的原因。感谢所有尝试帮助我的人。