Home > Software engineering >  cant automate calendar in selenium python
cant automate calendar in selenium python

Time:06-23

I 'm trying to automate a calendar with selenium, but cant find a way through, structure of the calendar html is like this:

img

6 divs containing a tags of each day, some days are disabled like this:

calendar

my take on this is that i'll select all non disabled a tags and look over then and if elem.text is equal to the certain date which i'll provide in function as a parameter, selenium will select that date this is my code for this:

    def pick_date(self, date, month, year):
        month_status = WebDriverWait(self.driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, '//*[@id="caltitle"]')))
        _month, _year = (month_status.text).split()
        next_month_btn = WebDriverWait(self.driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, '//*[@id="next-month"]')))
        all_active_dates = self.driver.find_elements(By.XPATH, '//*[@id="calweeks"]/div')
        print(_month, _year)
        for i, week_row in enumerate(all_active_dates):
            for day in week_row.find_elements(By.XPATH, f'//*[@id="calweeks"]/div[{i   1}]/a[not(@attr="caldisabled") and not(@attr="caloff")]'):
                if day.text == date:
                    day.click()
                print(day.text)

however for day in week_row.find_elements(By.XPATH, f'//*[@id="calweeks"]/div[{i 1}]/a[not(@attr="caldisabled") and not(@attr="caloff")]')

this is selecting all the a tags and not only the non disables output is something like this:

June 2022
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9

any help would be appreciated, coz i am bashing my head on this issue for soo long.

CodePudding user response:

Get all active elements:

all_active_dates = self.driver.find_elements(By.XPATH, f'//*[@id="calweeks"]//a[not(contains(@class,'caldisabled')) and not(contains(@class,'caloff'))]') 

and then just enumerate through all_active_dates and simply print(active_date.text)

  • Related