【问题标题】:Select row from table in Selenium based on certain text/values根据某些文本/值从 Selenium 中的表中选择行
【发布时间】:2021-05-17 02:55:43
【问题描述】:

我希望能够点击符合 2 个条件的行的任何部分:

  1. 我设置的时间段(即下午 5:30 - 6:30)
  2. 可以根据价值 Co-Ed 或 Women's Only 进行过滤

不确定如何过滤这些值。我虽然可能会搜索行列表的值

    timeslot = driver.find_elements_by_class_name("row.c-schedule-calendar__class-schedule-listitem-wrapper.c-schedule-calendar__workout-schedule-list-item")

下面是html代码

<li tabindex="0" class="row c-schedule-calendar__class-schedule-listitem-wrapper c-schedule-calendar__workout-schedule-list-item" data-index="22" data-workout-id="156986" data-club-id="204">
                <div class="col-md-12 col-lg-2 time-duration clickable js-single-class-list-item">
                    <span class="js-class-time">7:45 pm</span> <span class="dot-separator">-</span> <span class="js-class-duration">8:45 pm</span>
                </div>
                <div class="col-md-12 col-lg-10">
                    <div class="row">
                        <div class="col-8 clickable js-single-class-list-item">
                            <div class="row">
                                <div class="col-md-12 col-lg-6">
                                    <div class="class-name"><span class="js-class-name">General Workout Area</span></div>
                                    <div class="class-short-info">
                                        <span class="js-class-type">Co-ed</span>
                                    </div> 
                                </div>
                                <div class="col-md-12 col-lg-6">
                                    <div class="class-address-wrapper">
                                        <span class="class-address">
                                            <svg viewBox="0 0 48 48" class="c-search-class-filter--location__pin-icon">
                                            <use xlink:href="/etc.clientlibs/goodlife/clientlibs/clientlib-site/resources/images/icons.svg#ic_pin_default"></use>
                                            </svg> Cambridge Hespeler And Eagle
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                            <div class="col-4 c-schedule-calendar__cta-container">
                                <div class="js-class-action-container">
                                            <button class="c-btn-outlined class-action" data-class-action="book-class" data-class-action-step="class-action-confirmation" data-workout-id="156986" data-club-id="204" data-waitlistable="false"><span class="c-btn__label">Book</span></button>
                                </div>
                                <div class="available-spots">
                                            <div class="js-available-spots-container">
                                                2 spots left
                                            </div>
                            </div>
                    </div>
                </div>
            </div></li>

【问题讨论】:

    标签: python selenium selenium-chromedriver


    【解决方案1】:
    time_range = "5:30 pm - 6:30 pm"
    category = "Co-Ed"
    "//li[descendant::text()='{}' and descendant::span[text()='{}'] and descendant::span[text()='{}']]".format(time_range.split(' - ')[0],time_range.split(' - ')[1], category)
    

    这个 xpath 应该可以工作,所以不要使用类元素搜索

     driver.find_element_by_xpath("//li[descendant::text()='{}' and descendant::span[text()='{}'] and descendant::span[text()='{}']]".format(time_range.split(' - ')[0],time_range.split(' - ')[1], category)).click()
    

    【讨论】:

    • selenium.common.exceptions.ElementNotInteractableException: 消息:元素不可交互
    • 你能给我页面的网址吗?也许这可以帮助quora.com/…
    • goodlifefitness.com/content/goodlife/en/… 我之前尝试过使用 time.sleep(5),也尝试过使用显示在列表顶部的时间段(早上 6 点 - 早上 7 点)
    • 我注意到我的回答中有错字span[text()='8:45 pm'] 应该是span[text()='{}'] 还要添加这个div(如果你在第一天等,则使用id 1),因为另一天可能会有相同的元素(顶部过滤器)//div[@id='day-number-1']/li[descendant::text()='{}' and descendant::span[text()='{}'] and descendant::span[text()='{}']]
    • 也许你等待的时间不够,我认为最好的方法是等待加载器使用这个link xpath 来消散,因为它是//div[@class='c-schedule-calendar']//div[@class='c-accent-loader']
    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多