You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB
Go

package main
import (
"fmt"
"log"
"os"
"time"
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/firefox"
)
const (
seleniumPath = "/home/uzivatel/Desktop/scraper/selenium-server.jar" // Adjust this path
geckoDriverPath = "/home/uzivatel/Desktop/scraper/geckodriver" // Adjust this path
port = 5553
)
func screenshotwebpage(targetURL string, targetPort int) {
opts := []selenium.ServiceOption{
selenium.StartFrameBuffer(), // Start an X frame buffer for the browser to run in
selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver
}
// Start a Selenium WebDriver server instance
service, err := selenium.NewSeleniumService(seleniumPath, port, opts...)
if err != nil {
log.Fatalf("Error starting the Selenium server: %v", err)
}
defer service.Stop()
// Connect to the WebDriver instance running locally
caps := selenium.Capabilities{"browserName": "firefox"}
firefoxCaps := firefox.Capabilities{
Args: []string{"--ignore-certificate-errors"},
}
caps.AddFirefox(firefoxCaps)
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
if err != nil {
log.Fatalf("Error connecting to the WebDriver server: %v", err)
}
defer wd.Quit()
// Check if the port is 443 for HTTPS
protocol := "http"
if targetPort == 443 {
protocol = "https"
}
// Open the target URL
wd.Get(protocol + "://" + targetURL + "/")
time.Sleep(2 * time.Second)
alert, err := wd.AlertText()
_ = alert
if err == nil {
// Accept the alert
err = wd.AcceptAlert()
if err != nil {
log.Fatalf("Error accepting the alert: %v", err)
}
}
// Take a screenshot
screenshot, err := wd.Screenshot()
if err != nil {
fmt.Println("Error taking screenshot: %v", err)
}
// Save the screenshot to a file
if err := os.WriteFile("/home/uzivatel/Desktop/scraper/screenshots/"+targetURL, screenshot, 0644); err != nil {
log.Fatalf("Error saving screenshot: %v", err)
}
}