python - 使用 Python 和 Selenium 仅输出选定索引的文本
问题描述
我在创建一个自动结帐过程的程序时遇到了一些麻烦。我正在使用 python 3 和 Selenium。该程序解析一系列日期,这些日期在页面上作为可用的四个可用插槽输出。如果当前页面上没有可用的,它将单击“下一步”按钮并搜索接下来的四个日期。如果它到达可用日期范围的末尾并且一无所获,它将等待 30 秒并重置并重新开始。
我已经完成了大部分工作,除了两个问题:
1) 我正在尝试添加一个参数,当包含该参数时,它将超越基本功能(即使用 Twilio 通过文本简单地通知用户),并完成完整的结帐过程。
这是我正在使用的python代码:
def find_available(args):
dates_available = True
spaces_free = False
free_spaces = ""
while not spaces_free:
while dates_available:
time.sleep(1.5)
spots = driver.find_elements_by_css_selector('.ss-carousel-item')
for spot_index, spot in zip(range(date_range), spots):
if spot.value_of_css_property('display') != 'none':
spot.click()
available_dates = driver.find_elements_by_css_selector('.Date-slot-container')
for available_date in available_dates:
if available_date.value_of_css_property('display') != 'none':
selected_spot = available_date.find_element_by_css_selector('#slot-container-UNATTENDED')
if 'No doorstep delivery' not in selected_spot.text:
free_spaces = selected_spot.text.replace('Select a time', '').strip()
spaces_free = True
else:
print(selected_spot.text.replace('Select a time', '').strip())
if spaces_free:
print('Slots Available!')
if args.checkout:
client.messages.create(to=to_mobilenumber,
from_=from_mobilenumber,
body=free_spaces)
driver.find_element_by_xpath("//*[contains(text(), 'Soonest available')]").click()
time.sleep(1.5)
driver.find_element_by_xpath("//input[@type='submit' and @value='Continue']").click()
print('Your order has been placed!')
else:
client.messages.create(to=to_mobilenumber,
from_=from_mobilenumber,
body=free_spaces)
print('Your order time will be held for the next hour. Check your date and confirm!')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="auto-checkout")
parser.add_argument('--checkout', '-c', action='store_true',
help="Select first available slot and checkout")
args = parser.parse_args()
find_available(args)
预期行为
如果程序使用'--checkout' 或'-c' 参数启动,那么一旦'spaces-free' 设置为true,它应该发送一个带有'free_spaces' 元素内的文本的文本。然后它应该进入下一个阶段,这将是选择包含文本“Soonest available”的单选按钮(如选择包含可用时隙的第一个可用单选按钮),然后单击继续按钮.
实际行为
该程序将运行,找到一个可用的时间段,然后简单地继续到接下来的几天,从不尝试选择单选按钮并在结帐过程中前进。
我究竟做错了什么?
任何帮助,将不胜感激。
解决方案
在我看来,您从未在循环内设置dates_available
为:False
while
while dates_available:
time.sleep(1.5)
spots = driver.find_elements_by_css_selector('.ss-carousel-item')
for spot_index, spot in zip(range(date_range), spots):
if spot.value_of_css_property('display') != 'none':
spot.click()
available_dates = driver.find_elements_by_css_selector('.Date-slot-container')
for available_date in available_dates:
if available_date.value_of_css_property('display') != 'none':
selected_spot = available_date.find_element_by_css_selector('#slot-container-UNATTENDED')
if 'No doorstep delivery' not in selected_spot.text:
free_spaces = selected_spot.text.replace('Select a time', '').strip()
spaces_free = True
else:
print(selected_spot.text.replace('Select a time', '').strip())
所以你永远不会退出while
循环。如果你不想重写整个逻辑,你可以dates_available = False
在你设置后立即设置spaces_free = True
。这将允许退出while
循环,但您可能也需要一break
两个来退出for
循环。
如果你想要一个故障安全行为,你应该为更小的函数重构你的代码,如果你只想要第一个可用的东西,你可以只return
使用第一个可用数据的函数。
可能是这样的?
def find_available(args):
def get_a_date():
while True:
time.sleep(1.5)
spots = driver.find_elements_by_css_selector('.ss-carousel-item')
for spot_index, spot in zip(range(date_range), spots):
if spot.value_of_css_property('display') != 'none':
spot.click()
available_dates = driver.find_elements_by_css_selector('.Date-slot-container')
for available_date in available_dates:
if available_date.value_of_css_property('display') != 'none':
selected_spot = available_date.find_element_by_css_selector('#slot-container-UNATTENDED')
if 'No doorstep delivery' not in selected_spot.text:
return selected_spot.text.replace('Select a time', '').strip()
else:
print(selected_spot.text.replace('Select a time', '').strip())
free_spaces = get_a_date()
print('Slots Available!')
if args.checkout:
client.messages.create(to=to_mobilenumber,
from_=from_mobilenumber,
body=free_spaces)
driver.find_element_by_xpath("//*[contains(text(), 'Soonest available')]").click()
time.sleep(1.5)
driver.find_element_by_xpath("//input[@type='submit' and @value='Continue']").click()
print('Your order has been placed!')
else:
client.messages.create(to=to_mobilenumber,
from_=from_mobilenumber,
body=free_spaces)
print('Your order time will be held for the next hour. Check your date and confirm!')
推荐阅读
- .net - 找不到临时文件夹 Amazon Auto Scaling
- sql - 自动为用户生成唯一昵称
- python - 从 tensorflow lite 模型预测总是得到 0
- asp.net - 如何将整个 azure 网站嵌入 WordPress 网站
- linux - 如何找到复制文件的来源?
- java - 当从回收站视图中删除数据时,将检查项目更改为在回收站视图中取消选中
- oracle - 计数所有数据,即使是零/0 值
- django - 如何在 docker 容器中正确运行 django 和 apache
- python - 在 Render.com 上的 Flask 应用程序中预加载 Pytorch 模型时工作人员超时
- java - 是否可以获得其他活动视图的参考?