import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class M365WordAutomation {
public static void main(String[] args) {
// Set the path to the chromedriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Initialize a new WebDriver instance
WebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.manage().window().maximize();
// Set a default implicit wait time
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
// Navigate to the Microsoft 365 login page
driver.get("https://www.office.com/");
// Click on the Sign in button
WebElement signInButton = driver.findElement(By.linkText("Sign in"));
signInButton.click();
// Enter your email and password to log in
// Note: You will need to replace these selectors and input values with the appropriate ones for your login page
WebElement emailField = driver.findElement(By.id("i0116"));
emailField.sendKeys("your-email@example.com");
WebElement nextButton = driver.findElement(By.id("idSIButton9"));
nextButton.click();
// Wait for the password field to be visible and enter the password
WebElement passwordField = driver.findElement(By.id("i0118"));
passwordField.sendKeys("your-password");
WebElement signInButtonPassword = driver.findElement(By.id("idSIButton9"));
signInButtonPassword.click();
// If there's a "Stay signed in?" prompt, handle it
WebElement staySignedInButton = driver.findElement(By.id("idSIButton9"));
staySignedInButton.click();
// Navigate to Word
WebElement wordApp = driver.findElement(By.cssSelector("a[data-task='Word']"));
wordApp.click();
// Wait for Word to open and click on 'New blank document'
WebElement newBlankDocument = driver.findElement(By.cssSelector("div[aria-label='New blank document']"));
newBlankDocument.click();
// Switch to the editor frame
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='Editor']")));
// Enter some text into the Word document
WebElement editorBody = driver.findElement(By.xpath("//body[@contenteditable='true']"));
editorBody.click();
editorBody.sendKeys("Hello, this is a test.");
} finally {
// Close the browser after a short wait
try {
Thread.sleep(5000); // Wait for 5 seconds to see the entered text
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.quit();
}
}
}
Month: June 2024
Navigate to the document
public class Microsoft365Automation {
public static void main(String[] args) throws InterruptedException {
// Login steps as shown above...
// Navigate to OneDrive or directly to the document
driver.get("https://onedrive.live.com/");
// Wait for the page to load
Thread.sleep(5000);
// Open the specific document (you may need to adjust the selectors)
WebElement documentLink = driver.findElement(By.linkText("DocumentName.docx"));
documentLink.click();
// Wait for the document to load
Thread.sleep(5000);
// Switch to the frame where the document is loaded if necessary
// This step might be required to interact with the document editor
// driver.switchTo().frame("officeFrame");
// Perform the action to write in the document
}
}
Edit the document using
public class Microsoft365Automation {
public static void main(String[] args) throws InterruptedException {
// Previous steps...
// For example, to type into the Word document:
WebElement editorBody = driver.findElement(By.cssSelector("div[contenteditable='true']"));
editorBody.sendKeys("Hello, this is a test text.");
// Or use Actions class to simulate typing
Actions actions = new Actions(driver);
actions.moveToElement(editorBody).click().sendKeys("Hello, this is a test text.").perform();
// Add additional interactions as needed
}
}