[英]I am using Python and Selenium in order to automate site testing and cant get selenium to submit a form
[英]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.