0

Problem

I'm trying to write a script that records a number of screenshots, but I'm having a problem naming my file.


Try

I'm trying to initial my i=0 and increment it as I go.

I kept getting 1 on all of them.

My image getting replace during save, and all I got at the end is 1.png.

I suppose to have 16 of them.


Code

    i = 0
    driver = self.driver
    driver.set_window_size(1920, 1080)
    driver.get(self.base_url + "/")
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.find_element_by_id("username").send_keys("[email protected]")
    driver.find_element_by_id("password").send_keys("admin")
    driver.find_element_by_xpath("//button[@type='submit']").click()
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    time.sleep(5)
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.get(self.base_url + "/account")
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.find_element_by_link_text("Create").click()
    time.sleep(1)
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.find_element_by_link_text("Cancel").click()
    time.sleep(1)
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.find_element_by_css_selector("i.fa.fa-trash-o").click()
    driver.find_element_by_link_text("Got it").click()
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.get(self.base_url + "/account/1002")
    time.sleep(2)
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.get(self.base_url + "/access-point")
    driver.save_screenshot(today+'/admin/0.png')
    driver.get(self.base_url + "/access-point/000D6751560C")
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.get(self.base_url + "/access-point/000D6751560C/08002785112C")
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.get(self.base_url + "/captive-portal/admin")
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.get(self.base_url + "/cloud-security")
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.get(self.base_url + "/setting/mirroring")
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
    driver.get(self.base_url + "/profile")
    driver.save_screenshot(today+'/admin/'+str(i+=1)+'.png')
1
  • I doubt your code even executes. Your str(i+=1) should raise a SyntaxError. i+=1 is an assignment command, not an expression. Commented Jul 1, 2016 at 13:09

2 Answers 2

2

You are not setting back to i. Hence i is always 0.
You need to increment i after saving screenshot.

driver.save_screenshot(today+'/admin/'+str(i+1)+'.png')
i += 1

Not sure how is your code desgined so I'm giving really basic starting point.
You can do something like this. This one assumes today is in a reachable scope.

def save_screenshot_with_increment(): #these parameters normally depends on your design
    global i
    driver.save_screenshot(today+'/admin/'+str(i+1)+'.png')
    i += 1

Now instead of calling driver.save_screenshot, you may call save_screenshot_with_increment

driver.get(self.base_url + "/cloud-security")
save_screenshot_with_increment()
Sign up to request clarification or add additional context in comments.

7 Comments

Do I have to keep this adding this line all the time i += 1 ?
Yes, else i will not change value.
Yes, if you want to increment after saving your screenshot each time
Ooo... function ... that sound cool. Do you mind get me a couple lines to start ?
I will accept your answer. But if you can add that function that will be perfect.
|
0

you need to increment i value for every iteration. like i=i+1

if not i value will be same for every iteration. i.e 1. thats why every time you are getting 1.png

2 Comments

I guess str(i+=1). gives you SyntaxError: invalid syntax
you dont you use loop for that something like for i in range(0,16): print(str(i)+".png")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.