Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

查找的元素不存在时,执行很慢,需要 10 秒 #209

Closed
bitxeno opened this issue May 1, 2024 · 2 comments
Closed

查找的元素不存在时,执行很慢,需要 10 秒 #209

bitxeno opened this issue May 1, 2024 · 2 comments

Comments

@bitxeno
Copy link

bitxeno commented May 1, 2024

环境:
Linux 5.4.162 #0 SMP Mon Jan 17 04:29:05 2022 x86_64 GNU/Linux
Python 3.11.9
DrissionPage 4.0.4.21

测试代码如下 :

from DrissionPage import ChromiumPage
from DrissionPage import ChromiumOptions
import logging

logger_format = '%(asctime)s %(levelname)-8s %(message)s'
logging.basicConfig(
    format=logger_format,
    level='DEBUG',
    datefmt='%Y-%m-%d %H:%M:%S'
)

co = ChromiumOptions()
co.set_argument('--incognito')
co.set_argument('--no-sandbox')
co.set_headless(True)
page = ChromiumPage(co)

url = "https://www.fanfiction.net/s/14145272/1/In-Your-Wildest-Dreams"
logging.debug(f'Navigating to... {url}')
page.get(url)
selector = ['#challenge-running', '#cf-challenge-running', '#cf-please-wait']
for selector in selector:
    logging.debug("checking selector: " + selector + " ...")
    found_elements = page.eles(selector)
    logging.debug("found elements: " + str(found_elements))

执行结果:

2024-05-01 14:27:42 DEBUG    Starting new HTTP connection (1): 127.0.0.1:9222
2024-05-01 14:27:42 DEBUG    http://127.0.0.1:9222 "GET /json HTTP/1.1" 200 1196
2024-05-01 14:27:42 DEBUG    Starting new HTTP connection (1): 127.0.0.1:9222
2024-05-01 14:27:42 DEBUG    http://127.0.0.1:9222 "GET /json/version HTTP/1.1" 200 404
2024-05-01 14:27:42 DEBUG    Starting new HTTP connection (1): 127.0.0.1:9222
2024-05-01 14:27:42 DEBUG    http://127.0.0.1:9222 "GET /json HTTP/1.1" 200 1196
2024-05-01 14:27:42 DEBUG    Navigating to... https://www.fanfiction.net/s/14145272/1/In-Your-Wildest-Dreams
2024-05-01 14:27:45 DEBUG    checking selector: #challenge-running ...
2024-05-01 14:27:45 DEBUG    found elements: [<ChromiumElement h2 id='challenge-running' class='h2'>]
2024-05-01 14:27:45 DEBUG    checking selector: #cf-challenge-running ...
2024-05-01 14:27:55 DEBUG    found elements: []
2024-05-01 14:27:55 DEBUG    checking selector: #cf-please-wait ...
2024-05-01 14:28:05 DEBUG    found elements: []

可以看到查找#cf-challenge-running#cf-please-wait这种不存在的元素时,结果日志输出需要 10 秒钟,但查找页面存在的元素#challenge-running时,执行就很快,不到 1 秒,请问这个是什么原因导致的?还能优化吗?

@songge8
Copy link

songge8 commented May 11, 2024

page.ele()是不是有个timeout参数?

@bitxeno
Copy link
Author

bitxeno commented May 16, 2024

可以切换到 s 模式解决 https://drissionpage.cn/get_start/concept#-s-%E6%A8%A1%E5%BC%8F

@bitxeno bitxeno closed this as completed May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants