简体   繁体   English

无法在 selenium python 中自动执行日历

[英]cant automate calendar in selenium python

I 'm trying to automate a calendar with selenium, but cant find a way through, structure of the calendar html is like this:我正在尝试使用 selenium 自动化日历,但找不到解决方法,日历 html 的结构是这样的:

图像

6 divs containing a tags of each day, some days are disabled like this: 6 个 div 包含每天的标签,有些日子像这样被禁用:

日历

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:我对此的看法是,我将选择所有未禁用的标签并查看,如果 elem.text 等于我将在函数中作为参数提供的某个日期,selenium 将选择该日期这是我的代码为了这:

    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")]')但是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:这是选择所有 a 标签,不仅非禁用输出是这样的:

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.任何帮助将不胜感激,因为我在这个问题上抨击了很长时间。

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)然后只需枚举all_active_dates并简单地print(active_date.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM