簡體   English   中英

無法在 selenium python 中自動執行日歷

[英]cant automate calendar in selenium python

我正在嘗試使用 selenium 自動化日歷,但找不到解決方法,日歷 html 的結構是這樣的:

圖像

6 個 div 包含每天的標簽,有些日子像這樣被禁用:

日歷

我對此的看法是,我將選擇所有未禁用的標簽並查看,如果 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)

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

這是選擇所有 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

任何幫助將不勝感激,因為我在這個問題上抨擊了很長時間。

獲取所有活動元素:

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

然后只需枚舉all_active_dates並簡單地print(active_date.text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM