如何向if语句添加save_screenshot?

时间:2019-05-21 11:30:05

标签: python selenium pycharm

如果要禁用登录按钮,我想截图。在哪里可以添加save_screenshot来做到这一点?

def is_login_button_disabled(self):
        self.disabled_login_button = self.browser.find_element(*self.login_button_disabled)
        if self.disabled_login_button.is_displayed():
            print("Login Button Disabled")
        else:
            print("Login Button Enabled")

谢谢。

1 个答案:

答案 0 :(得分:1)

只需添加一行即可:

def is_login_button_disabled(self):
    self.disabled_login_button = self.browser.find_element(*self.login_button_disabled)
    if self.disabled_login_button.is_displayed():
        # take screen shot
        self.browser.save_screenshot("screenshot.png")
        print("Login Button Disabled")
    else:
        print("Login Button Enabled")

编辑:

您应该使用额外的变量,例如image = "image"count=0变量,该变量在每次截屏后都会增加

image_screenshot = image + str(count) + ".png"
self.browser.save_screenshot(image_screenshot)
count+=1

注意:

在调用函数时定义图像并计数为全局变量或作为参数传递,并在主函数中进行定义,例如:

def main():
   image = "image"
   count = 0
   self.is_login_button_disabled(image,count)