Below are examples of how to zoom in or out of a web page content using Selenium WebDriver with `chromedriver` in Java, Python, and C#:
Python:
from selenium import webdriver
options = webdriver.ChromeOptions()
# Specify the path to chromedriver executable
driver = webdriver.Chrome(executable_path='/path/to/chromedriver', chrome_options=options)
# Open a webpage
driver.get("https://www.example.com")
# Zoom in (e.g., 1.5x)
driver.execute_script("document.body.style.zoom = '1.5'")
# Continue with your interactions or testing
# Close the browser
driver.quit()
Java:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ZoomExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
// Open a webpage
driver.get("https://www.example.com");
// Zoom in (e.g., 1.5x)
((JavascriptExecutor) driver).executeScript("document.body.style.zoom = '1.5'");
// Continue with your interactions or testing
// Close the browser
driver.quit();
}
}
C#:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class Program
{
static void Main()
{
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = "/path/to/chromedriver"; // Specify the path to chromedriver executable
IWebDriver driver = new ChromeDriver(options);
// Open a webpage
driver.Navigate().GoToUrl("https://www.example.com");
// Zoom in (e.g., 1.5x)
((IJavaScriptExecutor)driver).ExecuteScript("document.body.style.zoom = '1.5'");
// Continue with your interactions or testing
// Close the browser
driver.Quit();
}
}
These examples show how to zoom in or out of a web page’s content using Selenium WebDriver with `chromedriver` in different programming languages. You can adjust the zoom level as needed by modifying the value in the `executeScript` method. Make sure to specify the correct path to the `chromedriver` executable on your system.
Work with our skilled full stack experts to improve your software quality, accelerate testing cycles, and enhance overall performance.
Hire Full Stack Developers