我希望我的代码(见下文)获取每个Breakpoint的5个屏幕截图。但是在Breakpoint 1280的3个屏幕截图之后,它开始从下一个断点截取屏幕截图。
我做错了什么或我错过了什么?
#!/bin/sh
from datetime import datetime
from selenium import webdriver
from pyvirtualdisplay import Display
import time
from selenium.webdriver.chrome.options import Options
now = datetime.now().strftime('%d-%m-%Y_%H-%M-%S')
Breakpoint_1280 = 0
Breakpoint_768 = 0
Breakpoint_320 = 0
# Set Breakpoint for different View
Breakpoints = [1280, 768, 320]
for points in Breakpoints:
display = Display(visible=0, size=(points, 800))
display.start()
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.set_page_load_timeout(30)
driver.get('https://www.example.com')
driver.maximize_window()
# Taking Screnshoot in different Breakpoints
if points == 1280:
for a in Breakpoints:
Breakpoint_1280 += 1
print ("Taking %s. Screenshot for Breakpoint 1280" %(Breakpoint_1280))
driver.get_screenshot_as_file('Breakpoint_1280_Snap%s_%s.png' %(Breakpoint_1280, now))
time.sleep(10)
if Breakpoint_1280 == 5:
break
elif points == 768:
for b in Breakpoints:
Breakpoint_768 += 1
print ("Taking %s. Screenshot for Breakpoint 768" %(Breakpoint_768))
driver.get_screenshot_as_file('Breakpoint_768_Snap%s_%s.png' %(Breakpoint_768, now))
time.sleep(10)
if Breakpoint_768 == 5:
break
else:
for c in Breakpoints:
Breakpoint_320 += 1
print ("Taking %s. Screenshot for Breakpoint 320" %(Breakpoint_320))
driver.get_screenshot_as_file('Breakpoint_320_Snap%s_%s.png' %(Breakpoint_320, now))
time.sleep(10)
if Breakpoint_320 == 5:
break
display.stop()
希望有人可以帮助我..非常感谢:)。
答案 0 :(得分:0)
你的循环应该是这样的:
for i in range(0, 5):
.
.
答案 1 :(得分:0)
使用范围为5的循环来完成您的操作。
`范围内(5): 做点什么
表示范围(5)中的b: 做点什么
对于范围(5)中的c,: 做一点事 `
答案 2 :(得分:0)
正如已经评论过的那样,你正在迭代Breakpoints
只有三个元素,因此你的三个截图。您应该使用for
循环创建5个镜头。
您还可以通过在print语句和文件名中使用points
的值来大大简化代码:
from selenium.webdriver.chrome.options import Options
now = datetime.now().strftime('%d-%m-%Y_%H-%M-%S')
# Set Breakpoint for different View
Breakpoints = [1280, 768, 320]
for points in Breakpoints:
display = Display(visible=0, size=(points, 800))
display.start()
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.set_page_load_timeout(30)
driver.get('https://www.example.com')
driver.maximize_window()
# Taking Screnshoot in different Breakpoints
for i in range(5):
print ('Taking {}. Screenshot for Breakpoint {}'.format(i, points))
driver.get_screenshot_as_file('Breakpoint_{}_Snap{}_{}.png'.format(points, i, now))
time.sleep(10)
display.stop()
请注意,我还使用较新的format()
方法进行字符串格式化。