尝试在 Reddit 上自动截屏并面临问题

Trying to automate taking screenshot on Reddit and facing issues

提问人:codeme 提问时间:5/16/2023 最后编辑:James Zcodeme 更新时间:5/28/2023 访问量:89

问:

我正在尝试从我观看的 Youtube 视频中使 python 工作:使用 Python 制作自动 YouTube 视频并从那里获得参考。 我无法解决错误。作者已经将几个月前讨论的问题存档。我创建了一个名为 Screenshots 的新文件夹,以便 python 机器人可以保存它。现在我不知道该怎么办。

enter image description here

enter image description here

我尝试添加一个新文件夹并将等待秒数从 10 秒增加到 20 秒。

增加生成屏幕截图的等待时间

enter image description here

python-3.x python-2.7 selenium-webdriver

评论


答:

0赞 JB-007 5/28/2023 #1
#prelim
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
o = Options()
o.page_load_strategy = ['none', 'normal', 'eager'][1]
##use *normal* for implicit (this should be default anyways)
d = webdriver.Chrome(options = o)  #deprecated - services best practice



#(1) not most efficient way - use:
d.implictly_wait(30)
d.get('https://www.google.com')

#for screenshot
d.save_screenshot('Folder_Path/screenshot_1.png')
## no arguments i.e () to save to same folder as script - ta    

#(2)explicit wait (remove implicitly_wait above)
d.get('https://www.google.com')
try: WebDriverWait(d,20).until(EC.presence_of_element_located((By.ID,'ID_HERE')))
except: print('not found')
#put relevant AND unique ID (not present on pvs site) in place of *ID_HERE*
d.save_screenshot('Folder_Path/screenshot_2.png')

笔记: 根据要求进行定制。

这给出了两种方法 - implicitly_wait(在没有任何其他方法的情况下是默认的,但设置它允许用户定义的秒作为参数 - 和显式等待作为替代方法(使用预期条件) - 许多人说不要同时使用这两种方法,但我这样做没有遇到任何问题 - 隐式等待将等到页面加载(s.t.max秒),然后显式方法将启动(它们不能并且会据我所知,不能协同工作)。

故障排除可能涉及确保在使用显式方法时已选择要等待的适当元素。

也可以输入或等作为第一个显式等待条件,然后是上面的等待条件,如果您从一页到下一页找到通用元素 ID。EC.url_changesEC.url_to_be