kaliiiiiiiiii / Selenium-Driverless

undetected Selenium without usage of chromedriver
https://kaliiiiiiiiii.github.io/Selenium-Driverless/
Other
598 stars 69 forks source link

cdp_socket.exceptions.CDPError: {'code': -32000, 'message': 'Cannot find context with specified id'} #59

Closed ahmedabdelhamedz closed 1 year ago

ahmedabdelhamedz commented 1 year ago
from selenium_driverless import webdriver
from selenium_driverless.types.by import By
import numpy as np
import asyncio
import time
import requests
import random

pc=input('enter your ADS POWER profile code: ')
ads_id = pc

open_url = "http://local.adspower.net:50325/api/v1/browser/start?user_id=" + ads_id
close_url = "http://local.adspower.net:50325/api/v1/browser/stop?user_id=" + ads_id

resp = requests.get(open_url).json()
print(resp)

async def main():
    options = webdriver.ChromeOptions()
    options.debugger_address = resp["data"]["ws"]["selenium"]
    async with webdriver.Chrome(options=options) as driver:
        await driver.maximize_window()
        await driver.get('http://whoer.net',wait_load=True)
        input('enter to pass /whoer.net: ')
        await driver.get('http://nowsecure.nl#relax',wait_load=True)
        input('enter to pass nowsecure.nl: ')
        await driver.get('https://fingerprintjs.com/products/bot-detection',wait_load=True)
        input('enter to pass fingerprintjs.com: ')
        await driver.get('https://iphey.com',wait_load=True)
        input('enter to pass iphey: ')
        await driver.get('https://accounts.google.com',wait_load=True)
        input('enter to pass gmail: ')
        await driver.get("chrome://settings/clearBrowserData",wait_load=True)
        input('enter to pass clearBrowserData: ')
        await driver.get("https://google.com",wait_load=True)
        time.sleep(2)
        google_search = await driver.find_element(By.NAME,'q',timeout=300)
        time.sleep(2)
        await google_search.write('domain.com')
        timeDelay = random.randrange(1, 5)
        time.sleep(timeDelay)
        google_enter = await driver.find_element(By.CLASS_NAME, 'gNO89b',timeout=300)
        time.sleep(2)
        await google_enter.submit()
        timeDelay = random.randrange(1, 5)
        time.sleep(timeDelay)
        while True:
            window_height = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
            scroll = np.random.uniform(2, 69)
            await driver.execute_script("window.scrollBy(0, {})".format(scroll),timeout=300)
            window_height1 = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
            time.sleep(np.abs(np.random.normal(0.09, 0.08)))
            if window_height == window_height1: break
            if window_height1 > 1500: break

        while True:
            window_height = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
            scroll = np.random.uniform(-2, -69)
            await driver.execute_script("window.scrollBy(0, {})".format(scroll),timeout=300)
            window_height1 = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
            time.sleep(np.abs(np.random.normal(0.09, 0.08)))
            if window_height1 == 0: break
        google_result = await driver.find_element(By.CLASS_NAME, 'LC20lb MBeuO DKV0Md',timeout=300)
        time.sleep(2)
        await google_result.click()
        timeDelay = random.randrange(1, 5)
        time.sleep(timeDelay)
        while True:
            window_height = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
            scroll = np.random.uniform(2, 69)
            await driver.execute_script("window.scrollBy(0, {})".format(scroll),timeout=300)
            window_height1 = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
            time.sleep(np.abs(np.random.normal(0.09, 0.08)))
            if window_height == window_height1: break

        while True:
            window_height = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
            scroll = np.random.uniform(-2, -69)
            await driver.execute_script("window.scrollBy(0, {})".format(scroll),timeout=300)
            window_height1 = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
            time.sleep(np.abs(np.random.normal(0.09, 0.08)))
            if window_height1 == 0: break
        input()

asyncio.run(main())
requests.get(close_url)
C:\Users\samm\PycharmProjects\python\venv\Scripts\python.exe C:/Users/samm/PycharmProjects/python/main1.py
enter your ADS POWER profile code: jagmbp3
{'code': 0, 'msg': 'success', 'data': {'ws': {'puppeteer': 'ws://127.0.0.1:50673/devtools/browser/47aaddbd-48f2-4219-afac-4c9b2843bc40', 'selenium': '127.0.0.1:50673'}, 'debug_port': '50673', 'webdriver': 'C:\\Users\\samm\\AppData\\Roaming\\adspower_global\\cwd_global\\chrome_105\\chromedriver.exe'}}
enter to pass /whoer.net: 
enter to pass nowsecure.nl: 
enter to pass fingerprintjs.com: 
enter to pass iphey: 
enter to pass gmail: 
enter to pass clearBrowserData: 
Traceback (most recent call last):
  File "C:\Users\samm\PycharmProjects\python\main1.py", line 86, in <module>
    asyncio.run(main())
  File "C:\Users\samm\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\samm\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\samm\PycharmProjects\python\main1.py", line 69, in main
    window_height = await driver.execute_script("return document.documentElement.scrollTop",timeout=300)
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\webdriver.py", line 342, in execute_script
    return await target.execute_script(script, *args, max_depth=max_depth, serialization=serialization,
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\types\target.py", line 374, in execute_script
    res = await self.execute_raw_script(script, *args, max_depth=max_depth,
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\types\target.py", line 341, in execute_raw_script
    res = await self.execute_cdp_cmd("Runtime.callFunctionOn", args, timeout=timeout)
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\types\target.py", line 818, in execute_cdp_cmd
    result = await self.socket.exec(method=cmd, params=cmd_args, timeout=timeout)
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\cdp_socket\socket.py", line 69, in exec
    return await asyncio.wait_for(self._responses[_id], timeout=timeout)
  File "C:\Users\samm\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 481, in wait_for
    return fut.result()
cdp_socket.exceptions.CDPError: {'code': -32000, 'message': 'Cannot find context with specified id'}

Process finished with exit code 1

hello please i tried Selenium-Driverless library but when i use while loop to scroll down and up after it click on google search result it appear to me this error can i know how to solve a problem

kaliiiiiiiiii commented 1 year ago

@ahmedabdelhamedz I'm actually currently rewriting that logic for the next release. Stay tight therefore:)

kaliiiiiiiiii commented 1 year ago

@ahmedabdelhamedz This should be resolved with selenium-driverless==1.6 Please close this issue if it's fixed now:)

ahmedabdelhamedz commented 1 year ago

@ahmedabdelhamedz This should be resolved with selenium-driverless==1.6 Please close this issue if it's fixed now:)

i tried latest update [selenium-driverless==1.6.1] but it give me new error :

This package has a "Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)" Licence.
therefore, you'll have to ask the developer first, if you want to use this package for your buisiness.
https://github.com/kaliiiiiiiiii/Selenium-Driverless
Traceback (most recent call last):
  File "C:\Users\samm\PycharmProjects\python\main1.py", line 47, in <module>
    asyncio.run(main())
  File "C:\Users\samm\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\samm\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\samm\PycharmProjects\python\main1.py", line 30, in main
    em2 = await driver.find_element(By.NAME,'q')
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\webdriver.py", line 662, in find_element
    return await target.find_element(by=by, value=value, parent=parent, timeout=timeout)
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\types\target.py", line 523, in find_element
    return await parent.find_element(by=by, value=value, timeout=timeout)
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\types\webelement.py", line 250, in find_element
    return elems[idx]
TypeError: 'JSUnserializable' object is not subscriptable

Process finished with exit code 1

how can i solve this problem please ?

kaliiiiiiiiii commented 1 year ago

@ahmedabdelhamedz This should be resolved with selenium-driverless==1.6 Please close this issue if it's fixed now:)

i tried latest update [selenium-driverless==1.6.1] but it give me new error :

This package has a "Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)" Licence.
therefore, you'll have to ask the developer first, if you want to use this package for your buisiness.
https://github.com/kaliiiiiiiiii/Selenium-Driverless
Traceback (most recent call last):
  File "C:\Users\samm\PycharmProjects\python\main1.py", line 47, in <module>
    asyncio.run(main())
  File "C:\Users\samm\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\samm\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\samm\PycharmProjects\python\main1.py", line 30, in main
    em2 = await driver.find_element(By.NAME,'q')
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\webdriver.py", line 662, in find_element
    return await target.find_element(by=by, value=value, parent=parent, timeout=timeout)
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\types\target.py", line 523, in find_element
    return await parent.find_element(by=by, value=value, timeout=timeout)
  File "C:\Users\samm\PycharmProjects\python\venv\lib\site-packages\selenium_driverless\types\webelement.py", line 250, in find_element
    return elems[idx]
TypeError: 'JSUnserializable' object is not subscriptable

Process finished with exit code 1

how can i solve this problem please ?

Actually, that might be my bad. I'll give it a try:)

kaliiiiiiiiii commented 1 year ago

@ahmedabdelhamedz The followimg code:

async def main():
    options = webdriver.ChromeOptions()
    async with webdriver.Chrome(options=options) as driver:
        await driver.maximize_window()
        await driver.get("https://google.com", wait_load=False)
        reject_cookies = await driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[3]/span/div/div/div/div[3]/div[1]/button[1]/div", timeout=30)
        await asyncio.sleep(1)
        await reject_cookies.click()
        google_search = await driver.find_element(By.NAME, 'q', timeout=300)
        await google_search.write('domain.com')
        google_enter = await driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]', timeout=10)

works for me just fine. Closing therefore for now.

If you find any other bugs, feel free to report them with a minimum reporducible example

EDIT: Your original issue might be: https://github.com/kaliiiiiiiiii/Selenium-Driverless/issues/81

ahmedabdelhamedz commented 1 year ago

@ahmedabdelhamedz The followimg code:

async def main():
    options = webdriver.ChromeOptions()
    async with webdriver.Chrome(options=options) as driver:
        await driver.maximize_window()
        await driver.get("https://google.com", wait_load=False)
        reject_cookies = await driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[3]/span/div/div/div/div[3]/div[1]/button[1]/div", timeout=30)
        await asyncio.sleep(1)
        await reject_cookies.click()
        google_search = await driver.find_element(By.NAME, 'q', timeout=300)
        await google_search.write('domain.com')
        google_enter = await driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]', timeout=10)

works for me just fine. Closing therefore for now.

If you find any other bugs, feel free to report them with a minimum reporducible example

EDIT: Your original issue might be: #81

i tried your code as following

` from selenium_driverless import webdriver from selenium_driverless.types.by import By import asyncio import time import numpy as np

async def main(): options = webdriver.ChromeOptions() async with webdriver.Chrome(options=options) as driver: await driver.maximize_window() await driver.get("https://google.com", wait_load=False) reject_cookies = await driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[3]/span/div/div/div/div[3]/div[1]/button[1]/div", timeout=30) await asyncio.sleep(1) await reject_cookies.click() google_search = await driver.find_element(By.NAME, 'q', timeout=300) await google_search.write('domain.com') google_enter = await driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]', timeout=10) input() asyncio.run(main())`

i got the same error TypeError: 'JSUnserializable' object is not subscriptable

is there any solution? i think there wAS PROblem to find elements

kaliiiiiiiiii commented 1 year ago

@ahmedabdelhamedz can you try do:

elems = await driver.find_elements(By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]')
await elems.__exec__("console.log(obj)")

and then provide a screenshot of what you got in the developer-console ?

ahmedabdelhamedz commented 1 year ago

@ahmedabdelhamedz can you try do:

elems = await driver.find_elements(By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]')
await elems.__exec__("console.log(obj)")

and then provide a screenshot of what you got in the developer-console ?

1 d6dc1cc1-dc70-4d13-a85b-07fe08e5e920

i upload two screen shots first run code and second screenshot run code in debug mode in pycharm

kaliiiiiiiiii commented 1 year ago

@ahmedabdelhamedz can you try do:

elems = await driver.find_elements(By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]')
await elems.__exec__("console.log(obj)")

and then provide a screenshot of what you got in the developer-console ?

1 d6dc1cc1-dc70-4d13-a85b-07fe08e5e920

i upload two screen shots first run code and second screenshot run code in debug mode in pycharm

Thanks! I meant the developer console in chrome tho haha!

ahmedabdelhamedz commented 1 year ago

40243930-209c-466a-9bc8-5813e673dfc4 haha sorry brother i upload the screenshot of developer console in chrome i appreciate your help and support