在Python 3中使用solenium获取匹配表信息

问题描述 投票:0回答:1

我正在用python 3.8中的solenium编写一个web自动化机器人,从管理界面拉取销售数据,通过管理界面发送消息,感谢他们的购买。销售数据保存在一个表中,结构如下。

<tr id="sale_row">
  <td>Month | Day | Year | time of sale</td>
  <td>Customer name | <a href="Link to message customer">Message Customer</a></td>
  <td>item Purchased</td>
  <td>Purchase Amount</td>
</tr>

我只想给最近x天内的订单发送消息(这就是导致问题的原因,因为我似乎无法将日期与href挂钩)。

到目前为止,我只选择了我想检索的日期,但无法解决如何将第二个包含href的td发送到消息系统。

为了匹配日期,我做了一个与时间戳格式相匹配的字符串,然后在想要的天数范围内运行一个for循环,并将输出结果添加到一个列表中,另一个循环在页面中运行,拉出日期,然后将它们与想要的日期列表进行比较并检查是否匹配。(日期被保存在第1个td中,它是唯一一个有 "text-left "类的日期。)

    cm = today_date.strftime("%b")
    cd = today_date.day
    target_day = cd - 7
    days = range(target_day,cd+1)    
    sold_month = bot.find_elements_by_class_name("text-left")
    target_days = []
    active_sales =[]
    for day in days:
        target_date = target_days.append(cm+' '+str(day)+','+' '+str(cy))
    for month in sold_month:
        sale=active_sales.append(month.text[:-10]) 
    for active_sale in active_sales: 
        #print("Active sale:",active_sale)
        for target_day in target_days:
         #   print("Target Day:",target_days)
            if target_day == active_sale:
                print("Result:", active_sale)

上面提供的只选择想要的天数没有问题。为了在第二个td上得到消息系统的href,我在最后的if语句中尝试了以下内容,它返回了每个销售的所有链接,这是有意义的,但我似乎无法解决如何在第一个td在日期范围内的基础上从第二个td中得到消息系统的href。

sales = bot.find_elements_by_xpath('//a[contains(@href, "/Inbox/New/")]')

所有的消息链接都是不同的,因为它们包含客户ID,这就是为什么它们需要与日期相匹配。

python-3.x matching webautomation
1个回答
0
投票

通过xpath获取tr元素,然后循环获取每个tr中的td。然后切开子字符串,从每个td中获取我需要的信息。

© www.soinside.com 2019 - 2024. All rights reserved.