【问题标题】:Selenium Unable to click the button when searched by XPATHSelenium XPATH 搜索时无法点击按钮
【发布时间】:2022-10-03 01:19:23
【问题描述】:

我正在尝试在表中搜索类“itemavailable”的元素。如果它存在,则单击它,如果不存在则转到下一行并在列中再次搜索。

这是HTML代码。

我的搜索代码在这里点击这里。我已经计算了行和列,还构建了 FinalXpath

for t_row in range(2, (num_rows)):
    for t_col in range (3, 4):
        FinalXPath = before_XPath + str(t_row) + aftertd_XPath + str(t_col) + aftertr_XPath + "[@class='itemavailable']" 
        print(FinalXPath)
        try:
            if driver.find_element(By.XPATH,"FinalXPath"):
                print("found")
                avslot = driver.find_element_by_xpath(FinalXPath)
                avslot.click()
                slot_found = True
            break
        except NoSuchElementException:
            print("not matched")
            pass

输出如下。

//*[@id='slotsGrid']/tbody/tr[2]/td[3][@class='itemavailable']
not matched
//*[@id='slotsGrid']/tbody/tr[3]/td[3][@class='itemavailable']
not matched
//*[@id='slotsGrid']/tbody/tr[4]/td[3][@class='itemavailable']
not matched
//*[@id='slotsGrid']/tbody/tr[5]/td[3][@class='itemavailable']
not matched
//*[@id='slotsGrid']/tbody/tr[6]/td[3][@class='itemavailable']
not matched
//*[@id='slotsGrid']/tbody/tr[7]/td[3][@class='itemavailable']
not matched
//*[@id='slotsGrid']/tbody/tr[8]/td[3][@class='itemavailable']
not matched
//*[@id='slotsGrid']/tbody/tr[9]/td[3][@class='itemavailable']
not matched
//*[@id='slotsGrid']/tbody/tr[10]/td[3][@class='itemavailable']
not matched

有一场比赛,但不知道为什么它会通过它。

【问题讨论】:

  • 你能分享一个指向那个页面的链接吗?
  • 不可以分享页面,抱歉
  • 就像现在一样,您的问题不符合最低可重现示例标准。

标签: python selenium selenium-webdriver


【解决方案1】:

它可能不是最好的或最优化的解决方案,但我会使用find_elements_by_xpath 函数(复数),它返回一个列表。
如果列表不为空,您可以单击第一个元素。使用这种方法,可以避免使用NoSuchElementException

if len(results) > 0
    results[0].click()

【讨论】:

    【解决方案2】:

    如果,如您所说,您只需要在给定表中查找类 itemavailable 的元素,并且如果表结构与您显示的一样,那么使用通过 id 和类名进行搜索会更容易:

    grid = driver.find_element(By.ID, 'slotsGrid')
    items = grid.find_elements(By.CLASS_NAME, 'itemavalible')
    
    for item in items:
         item.click()
    

    你的桌子:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <div class="table-grid">
            <table class="masterTable table table-narrow" id="slotsGrid">
                <tbody>
                    <tr></tr>
                    <tr>
                        <td class="masterTableLeftHeader">9:00</td>
                        <td class="itemnotavalible">...</td>
                        <td class="itemavalible">
                            <span class="buttonwrapper"><a href="http://" target="_blank">item-1</a></span>
                        </td>
                        <td class="itemnotavalible">...</td>
                        <td class="itemnotavalible">...</td>
                        <td class="itemavalible">
                            <span class="buttonwrapper"><a href="http://" target="_blank">item-2</a></span>
                        </td>
                        <td class="itemnotavalible">...</td>
                        <td class="itemnotavalible">...</td>
                    </tr>
                    <tr>
                        <td class="masterTableLeftHeader">9:00</td>
                        <td class="itemnotavalible">...</td>
                        <td class="itemavalible">
                            <span class="buttonwrapper"><a href="http://" target="_blank">item-3</a></span>
                        </td>
                        <td class="itemnotavalible">...</td>
                        <td class="itemnotavalible">...</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      相关资源
      最近更新 更多