import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from datetime import datetime
def open_and_click_button(url, username, password):
try:
# Initialize Chrome webdriver in headless mode
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
# Open the webpage
driver.get(url)
# Wait for the username input field to be visible and fill it
username_field = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'mat-input-0'))
)
username_field.send_keys(username)
# Wait for the password input field to be visible and fill it
password_field = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'mat-input-1'))
)
password_field.send_keys(password)
# Find the login button by class name and click it
login_button = driver.find_element(By.CLASS_NAME, 'mat-button-wrapper')
login_button.click()
# Wait for the Reports button to become clickable
# Locate the element by its class and alt attributes
reports_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//img[@alt="Reports" and contains(@src, "appmenu_reports_24.svg")]'))
)
# Click on the Reports button using JavaScript
driver.execute_script("arguments[0].click();", reports_button)
# Wait for the AR button to become clickable
ar_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/app-root/div/app-layout/app-header/mat-sidenav-container/mat-sidenav-content/div/app-report-view/div[3]/app-inview-grid/ag-grid-angular/div/div[2]/div[2]/div[3]/div[1]/div/div[2]/div/div/div[4]/div[2]/span/span[4]/app-report-link-renderer/a'))
)
# Click on the AR button using JavaScript
driver.execute_script("arguments[0].click();", ar_button)
# Get the current date
current_date = datetime.now()
# Format the date as "YYYY-MM-01"
first_day_of_month = current_date.strftime("%Y-%m-01")
# Execute JavaScript to set the value attribute of the input element
date_input = driver.find_element_by_xpath('//*[@id="ReportViewerControl_ctl04_ctl03_txtValue"]')
driver.execute_script("arguments[0].setAttribute('value', arguments[1])", date_input, first_day_of_month)
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Close the webdriver after 10 seconds
time.sleep(10)
driver.quit()
# Example usage:
url = "X"
username = "X"
password = "X"
open_and_click_button(url, username, password)
This is my code, it manages to login okay, click both the reports and then the ar button to get to a page, which I believe is a Microsoft Report Builder page, I then want Python to automatically type in the first date of the previous month into the start date and the last date of the previous month into the end date. I then want to untick some options from the available drop downs then download the report.
It only gets to the page with the report builder then it doesnt fill in the dates, I havent tried coding the drop down bits yet but looking for some help on this.
I have tried the code I have shown but it doesnt work, can someone help please, below is a screengrab of what it looks like on the page if that helps.enter image description here