kaliiiiiiiiii / Selenium-Driverless

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

Elements cannot be discovered (selenium-driverless 1.7) #133

Closed User-Clb closed 7 months ago

User-Clb commented 7 months ago
from selenium_driverless.sync import webdriver
from selenium_driverless.utils.utils import read
from selenium_driverless.types.by import By
from selenium_driverless import webdriver
from selenium_driverless.webdriver import WebElement
import asyncio

async def target_1_handler(target:WebElement):
    await target.get('https://abrahamjuliot.github.io/creepjs/')
    print(await target.title)
    print(await target.find_element(By.XPATH,'//*[@id="fingerprint-data"]/div[2]/div/div[1]/div[1]/span'))

async def target_2_handler(target:WebElement):
    await target.get("https://pypi.org/project/selenium-driverless/#description")
    print(await target.title)
    print(await target.find_element(By.XPATH,'//*[@id="description"]/div/h1'))

async def main():
    options = webdriver.ChromeOptions()
    async with webdriver.Chrome(options=options) as driver:
        target_1 = await driver.current_target
        target_2 = await driver.new_window("tab", activate=False)
        await asyncio.gather(
            target_1_handler(target_1),
            target_2_handler(target_2)
        )
        await target_1.focus()
        input("press ENTER to exit")

asyncio.run(main())
Traceback (most recent call last):
  File "c:\xx\xx\xx\xx\xx.py", line 34, in <module>
    asyncio.run(main())
  File "D:\anaconda3\envs\xxx\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "D:\anaconda3\envs\xxx\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "c:\xx\xxx\xx\xx\xx.py", line 26, in main
    await asyncio.gather(
  File "c:\xx\xx\xx\xx\xx.py", line 12, in target_1_handler
    print(await target.find_element(By.XPATH,'//*[@id="fingerprint-data"]/div[2]/div/div[1]/div[1]/span'))
  File "D:\anaconda3\envs\xxx\lib\site-packages\selenium_driverless\types\target.py", line 585, in find_element
    raise NoSuchElementException()
selenium_driverless.types.webelement.NoSuchElementException
kaliiiiiiiiii commented 7 months ago

Following works just fine:

from selenium_driverless.sync import webdriver
from selenium_driverless.types.by import By
from selenium_driverless import webdriver
import asyncio
import re

async def target_1_handler(target):
    await target.get('https://abrahamjuliot.github.io/creepjs/')
    print(await target.title)
    elem = await target.find_element(By.XPATH, '//*[@id="fingerprint-data"]/div[2]/div/div[1]/div[1]/span', timeout=20)
    await asyncio.sleep(1)
    elem = await target.find_element(By.XPATH, '//*[@id="fingerprint-data"]/div[2]/div/div[1]/div[1]/span', timeout=20)
    text = await elem.text
    [(percent, letter)] = re.findall(r"(\d{1,3}\.?\d?)% ([A-Z])", text)
    percent = float(percent)
    print(f"got {percent}% ({letter})")

async def target_2_handler(target):
    await target.get("https://pypi.org/project/selenium-driverless/#description")
    print(await target.title)
    print(await target.find_element(By.XPATH,'//*[@id="description"]/div/h1'))

async def main():
    options = webdriver.ChromeOptions()
    async with webdriver.Chrome(options=options) as driver:
        target_1 = await driver.current_target
        target_2 = await driver.new_window("tab", activate=False)
        await asyncio.gather(
            target_1_handler(target_1),
            target_2_handler(target_2)
        )
        await target_1.focus()
        input("press ENTER to exit")

asyncio.run(main())

@User-Clb This is a warning! Please don't open any issues unless there's a bug. I don't have the resources to fix user-side issues.