Search code examples
excelvbaselenium

Take screenshot using selenium VBA


I know how to take screenshot for specific element from the webpage. I tried several lines of code to catch the image but no one of them succeeds to capture the image well. Here's the link of the site https://eservices.moj.gov.kw/searchPages/searchCases.jsp Here's my code and tries as for this part

   .Window.Maximize 
   .Get sURL
    
    
    .Wait 2000
    '.FindElementById("txtCaseNo").SendKeys "192883660"
    'DecryptCaptcha
    Dim obj As Object
    '.FindElementByXPath("//*[@id='viewPane']/div").ScrollIntoView True
    .FindElementById("viewPane").ScrollIntoView True
    .Wait 2000
    '.FindElementByXPath("//div/img[@src='/captcha/imgCaptcha.jsp']").ScrollIntoView True
    '.FindElementByXPath("//div/img[@src='/captcha/imgCaptcha.jsp']").ScrollIntoView True
    
    
                'Set obj = .FindElementByXPath("//div/img[@src='/captcha/imgCaptcha.jsp']").TakeScreenshot(3000)
Set obj = .TakeScreenshot(3000)
        obj.SaveAs (ThisWorkbook.Path + "\Number.png")
    .Wait 2000

I could only capture the whole page but I want to capture the element of captcha only.


Solution

  • This is the working code that solved my problem

    Private bot As Selenium.ChromeDriver
    
    Sub Test()
        Dim element As Object
        Set bot = New Selenium.ChromeDriver
        With bot
            .Start
            .Get "https://eservices.moj.gov.kw/searchPages/searchCases.jsp"
            Set element = .FindElementByXPath("//*[@id='frmCaseNo']/div[2]/img")
            .ExecuteScript "arguments[0].scrollIntoView();", element
            element.TakeScreenshot(3000).SaveAs (ThisWorkbook.Path + "\Number.png")
        End With
    End Sub