Webdriver Screenshot

后端 未结 12 765
南方客
南方客 2020-11-30 00:57

When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file t

相关标签:
12条回答
  • 2020-11-30 01:26
    TakeScreenShot screenshot=new TakeScreenShot();
    screenshot.screenShot("screenshots//TestScreenshot//password.png");
    

    it will work , please try.

    0 讨论(0)
  • 2020-11-30 01:29

    Use driver.save_screenshot('/path/to/file') or driver.get_screenshot_as_file('/path/to/file'):

    import selenium.webdriver as webdriver
    import contextlib
    
    @contextlib.contextmanager
    def quitting(thing):
        yield thing
        thing.quit()
    
    with quitting(webdriver.Firefox()) as driver:
        driver.implicitly_wait(10)
        driver.get('http://www.google.com')
        driver.get_screenshot_as_file('/tmp/google.png') 
        # driver.save_screenshot('/tmp/google.png')
    
    0 讨论(0)
  • 2020-11-30 01:29

    Have a look on the below python script to take snap of FB homepage by using selenium package of Chrome web driver.

    Script:

    import selenium

    from selenium import webdriver

    import time

    from time import sleep

    chrome_browser = webdriver.Chrome()

    chrome_browser.get('https://www.facebook.com/') # Enter to FB login page

    sleep(5)

    chrome_browser.save_screenshot('C:/Users/user/Desktop/demo.png') # To take FB homepage snap

    chrome_browser.close() # To Close the driver connection

    chrome_browser.quit() # To Close the browser

    0 讨论(0)
  • 2020-11-30 01:29

    I understand you are looking for an answer in python, but here is how one would do it in ruby..

    http://watirwebdriver.com/screenshots/

    If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.

    eg:

     image = b.screenshot.png
    
     File.open("testfile.png", "w") do |file|
      file.puts "#{image}"
     end
    

    where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.

    0 讨论(0)
  • 2020-11-30 01:31

    This will take screenshot and place it in a directory of a chosen name.

    import os
    driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
    
    0 讨论(0)
  • 2020-11-30 01:33

    Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver

    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('http://www.google.com/')
    browser.save_screenshot('screenie.png')
    browser.quit()
    
    0 讨论(0)
提交回复
热议问题