Code: Select all
Exception in thread Thread-1 (_recv_loop):
Traceback (most recent call last):
File "C:\Users\Administrator2\AppData\Local\Programs\Python\Python313\Lib\threading.py", line 1041, in _bootstrap_inner
self.run()
> ~~~~~~~~^^
File "C:\Users\Administrator2\AppData\Local\Programs\Python\Python313\Lib\threading.py", line 992, in run
self._target(*self._args, **self._kwargs)
> ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator2\AppData\Local\Programs\Python\Python313\Lib\site-packages\pychrome\tab.py", line 122, in _recv_loop
message = json.loads(message_json)
File "C:\Users\Administrator2\AppData\Local\Programs\Python\Python313\Lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
> ~~~~~~~~~~~~~~~~~~~~~~~^^^
File "C:\Users\Administrator2\AppData\Local\Programs\Python\Python313\Lib\json\decoder.py", line 345, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
> ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator2\AppData\Local\Programs\Python\Python313\Lib\json\decoder.py", line 363, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Hier ist alles Skript, das ich verwende:
Code: Select all
import subprocess
import time
import pychrome
# Start Chrome in remote debugging mode
chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
user_data_dir = r"C:\Users\Administrator2\AppData\Local\Google\Chrome\User Data"
remote_debugging_port = 9222
# Command to start Chrome
subprocess.Popen([chrome_path, f"--remote-debugging-port={remote_debugging_port}", f"--user-data-dir={user_data_dir}"])
# Wait a moment to ensure Chrome starts
time.sleep(5)
# Connect to Chrome
browser = pychrome.Browser(url=f"http://127.0.0.1:{remote_debugging_port}")
# Create a new tab
tab = browser.list_tab()[0] # Select the first tab
tab.start()
# Navigate to the page
tab.call_method("Page.navigate", url="here any website")
tab.call_method("Page.enable")
# Wait a moment for the page to load
time.sleep(5) # Adjust the wait time as necessary
# Wait until the desired element appears in the DOM
total_wait_time = 20 # Total wait time in seconds
chunk_wait_time = 5 # Time to wait for each chunk in seconds
elapsed_time = 0
while elapsed_time < total_wait_time:
time.sleep(chunk_wait_time) # Wait for the chunk time
elapsed_time += chunk_wait_time # Increment the elapsed time
# Evaluate the DOM to check if the element is present
element = tab.call_method("Runtime.evaluate", expression='document.querySelector("div.count.ng-star-inserted")')
if 'result' in element and element['result'] is not None:
# If the element is present, get its text
text_content = tab.call_method("Runtime.evaluate", expression='document.querySelector("div.count.ng-star-inserted").innerText')
print(f'Text in : {text_content["result"]["value"]}')
break
else:
print("Element not found within 20 seconds. Exiting script.")
# Close the tab properly
tab.stop()