0

Element of the Start Date box below

<input name="ReportViewerControl$ctl04$ctl03$txtValue" type="text" size="28" onchange="javascript:setTimeout('__doPostBack(\'ReportViewerControl$ctl04$ctl03$txtValue\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="ReportViewerControl_ctl04_ctl03_txtValue" class="null" tabindex="1">

XPath of Start Date box

//*[@id="ReportViewerControl_ctl04_ctl03_txtValue"]

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

5
  • 1
    It is good that you have provided your code and described the problem, but without seeing a sample of the HTML for the site or the actual site, it is not possible to say where you went wrong with your code.
    – Shatas
    CommentedJan 29, 2024 at 20:07
  • Okay thanks for your response, I will happily display all of the HTML code for the page if needed but I assume it would be more use if I just showed the elements I wish to work with I have added these to the postCommentedJan 30, 2024 at 22:40
  • why not use send_keys method?CommentedJan 30, 2024 at 22:41
  • I have tried the sendkeys method and it doesnt work, I perhaps am doing something wrong elsewhere in the code thats affecting it but the sendkeys was first thing I tried - could you perhaps suggest how you would write for the send_keys for the start date, maybe i messed it upCommentedJan 30, 2024 at 22:45
  • I asked for the full HTML because you said that nothing hapoens once you reach thereport builder. There might be various reasons, there might be a frame element, the ids might be dynamic, your xpaths in general might be wrong.
    – Shatas
    CommentedJan 31, 2024 at 16:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.