kaliiiiiiiiii / Selenium-Driverless

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

cdp_socket.exceptions.CDPError: {'code': -32000, 'message': 'Could not find node with given id'} #127

Closed pythondeveloperz closed 9 months ago

pythondeveloperz commented 10 months ago

hello please how can i solve this error the error appeared from line before last related to (element21)

platform : windows 11 python version 3.10 selenium-driverless : 1.7

from selenium_driverless import webdriver
from selenium_driverless.types.by import By
import asyncio
import time
async def main():
    options = webdriver.ChromeOptions()
    async with webdriver.Chrome(options=options) as driver:
        await driver.get('https://www.google.com/', wait_load=True)
        await driver.maximize_window()
        elements =   await driver.find_element(By.CSS_SELECTOR,'#APjFqb')
        print(elements)
        await elements.write('forex')
        elements1 = await driver.find_element(By.CSS_SELECTOR, 'body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.FPdoLc.lJ9FBc > center > input.gNO89b',timeout=300)
        await elements1.click()
        elements21 = await driver.find_element(By.CSS_SELECTOR, '#rso > div:nth-child(1) > div > div > div.kb0PBd.cvP2Ce.jGGQ5e > div > div > span > a > h3 > span',timeout=300)
        input('enter any key: ')

asyncio.run(main())
kaliiiiiiiiii commented 10 months ago

@pythondeveloperz Please provide the full stack

pythondeveloperz commented 10 months ago
elements21 = await driver.find_element(By.CSS_SELECTOR, '#rso > div:nth-child(1) > div > div > div.kb0PBd.cvP2Ce.jGGQ5e > div > div > span > a > h3 > span',timeout=300)
Traceback (most recent call last):
  File "C:\Users\pcs\PycharmProjects\pythonProject1\main.py", line 21, in <module>
    asyncio.run(main())
  File "C:\Users\pcs\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\pcs\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete
    return future.result()
  File "C:\Users\pcs\PycharmProjects\pythonProject1\main.py", line 15, in main
    elements21 = await driver.find_element(By.CSS_SELECTOR, '#rso > div:nth-child(1) > div > div > div.kb0PBd.cvP2Ce.jGGQ5e > div > div > span > a > h3 > span',timeout=300)
  File "C:\Users\pcs\PycharmProjects\pythonProject1\venv\lib\site-packages\selenium_driverless\webdriver.py", line 877, in find_element
    return await target.find_element(by=by, value=value, parent=parent, timeout=timeout)
  File "C:\Users\pcs\PycharmProjects\pythonProject1\venv\lib\site-packages\selenium_driverless\types\target.py", line 575, in find_element
    elem = await parent.find_element(by=by, value=value, timeout=None)
  File "C:\Users\pcs\PycharmProjects\pythonProject1\venv\lib\site-packages\selenium_driverless\types\webelement.py", line 252, in find_element
    elems = await self.find_elements(by=by, value=value)
  File "C:\Users\pcs\PycharmProjects\pythonProject1\venv\lib\site-packages\selenium_driverless\types\webelement.py", line 287, in find_elements
    res = await self.__target__.execute_cdp_cmd("DOM.querySelectorAll", {"nodeId": node_id,
  File "C:\Users\pcs\PycharmProjects\pythonProject1\venv\lib\site-packages\selenium_driverless\types\target.py", line 781, in execute_cdp_cmd
    result = await self.socket.exec(method=cmd, params=cmd_args, timeout=timeout)
  File "C:\Users\pcs\PycharmProjects\pythonProject1\venv\lib\site-packages\cdp_socket\socket.py", line 78, in exec
    raise e
  File "C:\Users\pcs\PycharmProjects\pythonProject1\venv\lib\site-packages\cdp_socket\socket.py", line 72, in exec
    res = await asyncio.wait_for(self._responses[_id], timeout=timeout)
  File "C:\Users\pcs\AppData\Local\Programs\Python\Python310\lib\asyncio\tasks.py", line 447, in wait_for
    return fut.result()
cdp_socket.exceptions.CDPError: {'code': -32000, 'message': 'Could not find node with given id'}

Process finished with exit code 1
kaliiiiiiiiii commented 10 months ago

@pythondeveloperz Ahh yep that's at https://github.com/kaliiiiiiiiii/Selenium-Driverless/blob/4be24f290fe2d9253bb573b4f2384e551e411835/src/selenium_driverless/types/webelement.py#L286-L289

should be catched and then raise a StaleElementReference to fix. This issue is related to https://github.com/kaliiiiiiiiii/Selenium-Driverless/discussions/119#discussioncomment-7689642

pythondeveloperz commented 10 months ago

@pythondeveloperz Ahh yep that's at

https://github.com/kaliiiiiiiiii/Selenium-Driverless/blob/4be24f290fe2d9253bb573b4f2384e551e411835/src/selenium_driverless/types/webelement.py#L286-L289

should be catched and then raise a StaleElementReference to fix. This issue is related to #119 (reply in thread)

sorry bro i dont understand you well is there something i must edit in my code or there is a problem on package you will fix in the next realse?

kaliiiiiiiiii commented 10 months ago

sorry bro i dont understand you well is there something i must edit in my code or there is a problem on package you will fix in the next realse? @pythondeveloperz Will be fixed in the next release

pythondeveloperz commented 10 months ago

sorry bro i dont understand you well is there something i must edit in my code or there is a problem on package you will fix in the next realse? @pythondeveloperz Will be fixed in the next release

okay thanks please how can i make scroll down by selenium-driverless package ?

xrkk commented 10 months ago

I found a tricky way to fix most of 'Could not find node with given id' errors: before doing some actions (like getting page_source or find some element), set tab._document_elem_ = None, so that the "local" tab._document_elem_ will be updated before your actions really taken.

BTY, scroll down could be done by tab.execute_script("window.scrollTo(0, document.body.scrollHeight+1000);") (I used +1000 to make sure tab really scrolled to end). Or, you can do it by finding the last <div> or <p> or other element that has valid rect attribute, and call div_last.scroll_to()

kaliiiiiiiiii commented 10 months ago

I found a tricky way to fix most of 'Could not find node with given id' errors: before doing some actions (like getting page_source or find some element), set tab._document_elem_ = None, so that the "local" tab._document_elem_ will be updated before your actions really taken.

Yep, but eventho that doesn work, it isn't optimized. I'll provide another solution for that.

BTY, scroll down could be done by tab.execute_script("window.scrollTo(0, document.body.scrollHeight+1000);") (I used +1000 to make sure tab really scrolled to end). Or, you can do it by finding the last <div> or <p> or other element that has valid rect attribute, and call div_last.scroll_to()

Jep, JavaScript is detectable this way tho. I'll be refactoring the scrolling anyways.

kaliiiiiiiiii commented 9 months ago

https://github.com/kaliiiiiiiiii/Selenium-Driverless/commit/81a0f7de84a7e0b97eb0b26be436e596d3d7326e#diff-eb355e1147cb9c933fac6232a336122a60ef8bafb5a2661496aeb58ba142f741L285-R287 should fix this

pythondeveloperz commented 9 months ago

81a0f7d#diff-eb355e1147cb9c933fac6232a336122a60ef8bafb5a2661496aeb58ba142f741L285-R287 should fix this

is there a new release after 1.7 ? or try it with this version?

kaliiiiiiiiii commented 9 months ago

81a0f7d#diff-eb355e1147cb9c933fac6232a336122a60ef8bafb5a2661496aeb58ba142f741L285-R287 should fix this

is there a new release after 1.7 ? or try it with this version?

Nope that's only in the dev-branch yet. see readme for installation

pythondeveloperz commented 9 months ago

81a0f7d#diff-eb355e1147cb9c933fac6232a336122a60ef8bafb5a2661496aeb58ba142f741L285-R287 should fix this

is there a new release after 1.7 ? or try it with this version?

Nope that's only in the dev-branch yet. see readme for installation

i installed the dev-branch through pip install https://github.com/kaliiiiiiiiii/Selenium-Driverless/archive/refs/heads/dev.zip

please how can i test the code in dev branch ?

kaliiiiiiiiii commented 9 months ago

i installed the dev-branch through pip install https://github.com/kaliiiiiiiiii/Selenium-Driverless/archive/refs/heads/dev.zip

please how can i test the code in dev branch ?

Haha wdym? Now it's installed - good to go lol

kaliiiiiiiiii commented 9 months ago

ahh heck almost forgot about releasing from the dev branch lol - I'll dl that asap

kaliiiiiiiiii commented 9 months ago

resloved by https://github.com/kaliiiiiiiiii/Selenium-Driverless/commit/81a0f7de84a7e0b97eb0b26be436e596d3d7326e#diff-eb355e1147cb9c933fac6232a336122a60ef8bafb5a2661496aeb58ba142f741L285-R287

pythondeveloperz commented 9 months ago

resloved by 81a0f7d#diff-eb355e1147cb9c933fac6232a336122a60ef8bafb5a2661496aeb58ba142f741L285-R287

i update package now to 1.7.1 and try the code it give me another error 👍

Traceback (most recent call last): File "C:\Users\HP\PycharmProjects\pythonProject\main.py", line 19, in asyncio.run(main()) File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run return loop.run_until_complete(main) File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete return future.result() File "C:\Users\HP\PycharmProjects\pythonProject\main.py", line 15, in main elements21 = await driver.find_element(By.CSS_SELECTOR, '#rso > div:nth-child(1) > div > div > div.kb0PBd.cvP2Ce.jGGQ5e > div > div > span > a > h3 > span',timeout=5) File "C:\Users\HP\PycharmProjects\pythonProject\venv\lib\site-packages\selenium_driverless\webdriver.py", line 889, in find_element return await target.find_element(by=by, value=value, parent=parent, timeout=timeout) File "C:\Users\HP\PycharmProjects\pythonProject\venv\lib\site-packages\selenium_driverless\types\target.py", line 580, in find_element raise NoSuchElementException() selenium_driverless.types.webelement.NoSuchElementException

Process finished with exit code 1

kaliiiiiiiiii commented 9 months ago

resloved by 81a0f7d#diff-eb355e1147cb9c933fac6232a336122a60ef8bafb5a2661496aeb58ba142f741L285-R287

i update package now to 1.7.1 and try the code it give me another error 👍

Traceback (most recent call last): File "C:\Users\HP\PycharmProjects\pythonProject\main.py", line 19, in asyncio.run(main()) File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run return loop.run_until_complete(main) File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete return future.result() File "C:\Users\HP\PycharmProjects\pythonProject\main.py", line 15, in main elements21 = await driver.find_element(By.CSS_SELECTOR, '#rso > div:nth-child(1) > div > div > div.kb0PBd.cvP2Ce.jGGQ5e > div > div > span > a > h3 > span',timeout=5) File "C:\Users\HP\PycharmProjects\pythonProject\venv\lib\site-packages\selenium_driverless\webdriver.py", line 889, in find_element return await target.find_element(by=by, value=value, parent=parent, timeout=timeout) File "C:\Users\HP\PycharmProjects\pythonProject\venv\lib\site-packages\selenium_driverless\types\target.py", line 580, in find_element raise NoSuchElementException() selenium_driverless.types.webelement.NoSuchElementException

Process finished with exit code 1

@pythondeveloperz Are you sure the selectors haven't changed since then?

pythondeveloperz commented 9 months ago

resloved by 81a0f7d#diff-eb355e1147cb9c933fac6232a336122a60ef8bafb5a2661496aeb58ba142f741L285-R287

i update package now to 1.7.1 and try the code it give me another error 👍 Traceback (most recent call last): File "C:\Users\HP\PycharmProjects\pythonProject\main.py", line 19, in asyncio.run(main()) File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run return loop.run_until_complete(main) File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete return future.result() File "C:\Users\HP\PycharmProjects\pythonProject\main.py", line 15, in main elements21 = await driver.find_element(By.CSS_SELECTOR, '#rso > div:nth-child(1) > div > div > div.kb0PBd.cvP2Ce.jGGQ5e > div > div > span > a > h3 > span',timeout=5) File "C:\Users\HP\PycharmProjects\pythonProject\venv\lib\site-packages\selenium_driverless\webdriver.py", line 889, in find_element return await target.find_element(by=by, value=value, parent=parent, timeout=timeout) File "C:\Users\HP\PycharmProjects\pythonProject\venv\lib\site-packages\selenium_driverless\types\target.py", line 580, in find_element raise NoSuchElementException() selenium_driverless.types.webelement.NoSuchElementException Process finished with exit code 1

@pythondeveloperz Are you sure the selectors haven't changed since then?

i tried it now it sometime work and sometime not i think is there a problem on loading element of webpage how can i detect through the package that all element of the webpage are loaded ?