0

I am trying to select "Custom range" in the date selector of this site: https://niftygateway.com/sitewide-activity

I have written the following code to make Selenium click on "Right now":

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

webdriver = webdriver.Chrome(ChromeDriverManager().install()) #executable_path=chromedriver_path
time.sleep(2)
webdriver.get('https://niftygateway.com/sitewide-activity')
time.sleep(3)

date_selector = webdriver.find_element_by_xpath('//*[@id="root"]/div/div/div[1]/div[2]/div/div/button/span')
date_selector.click()

But after this, how do I make it click on "Custom range"? When I "inspect element" I cannot see anything change in the path upon selecting "Custom range". So I am not able to find a way to automate a click on "Custom range".

Furthermore, how do I make it select a date range, say Nov 1 or Nov 5? Because when I select a custom date range myself, again nothing in the xpath or URL changes, so I am not sure how to automate this.

Kristada673
  • 3,512
  • 6
  • 39
  • 93

2 Answers2

2

You can target input fields that have value attribute to send the dates.

they should look like this

<input aria-invalid="false" type="text" class="MuiInputBase-input MuiFilledInput-input" value="11/02/2021">

We can use execute_script to directly send the values. There is no need to select a date by clicking in the calendar.

Code:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)

driver.get("https://niftygateway.com/sitewide-activity")

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-controls='simple-menu']"))).click()
time.sleep(2)

wait.until(EC.element_to_be_clickable((By.XPATH, "//p[text()='Custom range']//following-sibling::div"))).click()

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.MuiDialogContent-root")))

time.sleep(2)
start_to = wait.until(EC.element_to_be_clickable((By.XPATH, "//label[text()='Start Date']//following-sibling::div/input")))
driver.execute_script("arguments[0].setAttribute('value', '11/03/2021')", start_to)

time.sleep(2)
end_date = wait.until(EC.element_to_be_clickable((By.XPATH, "//label[text()='End Date']//following-sibling::div/input")))
driver.execute_script("arguments[0].setAttribute('value', '11/12/2021')", end_date)

time.sleep(2)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Save']"))).click()

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

PS time.sleep() is for visibility purpose, I have tested without it as well, it works. I would recommend once you test, remove time.sleep() since they are not recommended waits.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Awesome! I had no idea about these "target input fields". Where do you find them? For example, next I want to, say, select only "Sales" in the "Show" section on the right, and unselect the rest of the options there. Where can I find the target input fields of it? – Kristada673 Nov 13 '21 at 07:16
  • Sales is a checkbox. not input field. **Steps to check:** `Press F12 in Chrome` -> go to `element` section -> do a `CTRL + F` -> then paste the `xpath` and see, if your desired `element` is getting **highlighted** with `1/1` matching node. – cruisepandey Nov 13 '21 at 07:18
  • By the way, I noticed just now that your last line (i.e., the code to "Save" the custom dates) might not be working, as upon running your code, it doesn't show the results of the selected dates, rather it still shows the newest listings in the site. – Kristada673 Nov 13 '21 at 08:16
  • hmm.. yes in the automation window it does not select Custom range. – cruisepandey Nov 13 '21 at 14:34
0

To click on Custom range you need to induce WebDriverWait for the element_to_be_clickable() and you can the following Locator Strategies:

driver.get("https://niftygateway.com/sitewide-activity")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-controls='simple-menu']/span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[text()='Custom range']"))).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352