gpt4 book ai didi

python-3.x - Selenium 和异步

转载 作者:行者123 更新时间:2023-12-05 07:36:53 24 4
gpt4 key购买 nike

完成这项工作的最佳方法是什么?由于 Selenium 不能很好地与 asyncio 配合使用,因此机器人最终会死掉。它按预期工作,但最终会崩溃。我确实明白问题出在哪里,尤其是因为 Selenium 太重了,但是有什么好的解决方法吗?

我收到错误信息:任务已被销毁,但它正在等待中!

我没有别的,没有回溯。我必须重新启动机器人 (discord),它会再次工作一段时间。

@bot.command(pass_context=True)
async def weather(ctx, arg):
if ctx:
list = ('lf','oahu')
await bot.say('*Loading webdriver.....*')
if arg in list:
if arg == 'lf':
website = '--'
name = 'Los Feliz Map'
if arg == 'oahu':
website = '--'
name = 'Oahu Map'
load_site = webdriver.Chrome()
# load site on chrome
load_site.get(website)
await bot.say('*Loading ' + str(name) + '...*')
load_site.find_element_by_xpath('//*[@id="main-map"]/div[2]/div[1]/div/a[2]').click()
load_site.find_element_by_xpath('//*[@id="main-map"]/div[2]/div[2]').click()
load_site.find_element_by_xpath('//*[@id="main-map"]/div[2]/div[2]/div/form/div[3]/label[6]/div/span').click()
await asyncio.sleep(2) #sleep to ensure weather loads on site before screenshot
load_site.save_screenshot('weather.png')
await bot.say('*Taking screenshot and saving image....*')
await bot.send_file(ctx.message.channel, 'weather.png')
load_site.quit()
print('Succesfully sent image of ' + str(arg) + ' - ' + str(ctx.message.author.name))

我遗漏了该网站,因为它是私有(private)网站。

最佳答案

简短回答:不可能。如果您以某种方式设法让它发挥作用,它就不会很好地发挥作用。

长答案:Selenium 是一个阻塞 API,您不能真正将它与异步脚本一起使用。如果你真的想使用网络浏览器,你可以尝试找到一个替代品,比如 https://pypi.org/project/aioselenium/ .

关于python-3.x - Selenium 和异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48895857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com