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
    }
}