【问题标题】:Selenium Find element inside row [closed]Selenium 在行内查找元素[关闭]
【发布时间】:2021-12-19 16:30:41
【问题描述】:

HTML:

<table id="wanInstTable_tbl" width="100%" border="0" cellpadding="0" cellspacing="0">
  <tbody>
    <tr class="alignment_rule">
      <td><input style="width:null" class="NewDelbuttoncss" id="Newbutton" type="button" value="New" onclick="clickAdd('wanInstTable_head');"><input style="margin-left:9px;width:null" id="DeleteButton" class="NewDelbuttoncss" type="button" value="Delete"
          onclick="OnDeleteButtonClick('wanInstTable_head');"></td>
    </tr>
    <tr>
      <td class="button_spread"></td>
    </tr>
    <tr>
      <td id="wanInstTable_head">
        <table width="100%" class="tabal_bg" id="wanInstTable" cellspacing="1">
          <tbody>
            <tr class="head_title">
              <td class="" id="headwanInstTable_0_0"></td>
              <td class="align_center restrict_dir_ltr" id="headwanInstTable_0_1">Connection Name</td>
              <td class="align_center" id="headwanInstTable_0_2">VLAN/Priority</td>
              <td class="align_center" id="headwanInstTable_0_3">Protocol Type</td>
            </tr>
            <tr id="wanInstTable_record_no" class="tabal_01">
              <td align="center" class="" id="wanInstTable_0_0">--</td>
              <td align="center" class="align_center restrict_dir_ltr" id="wanInstTable_0_1">--</td>
              <td align="center" class="align_center" id="wanInstTable_0_2">--</td>
              <td align="center" class="align_center" id="wanInstTable_0_3">--</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>

我读过这个documentation:,但做不到。

我需要访问id=Newbuttonfirst Trfirs tdbutton clickable,我已经尝试了很长时间

【问题讨论】:

    标签: python selenium browser automation


    【解决方案1】:

    由于您没有共享您尝试过的代码以及确切的错误堆栈跟踪,我可能可以假设一些事情。

    如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

    你应该检查的xpath:

    //input[@class='NewDelbuttoncss' and @id='Newbutton' and @value='New' and contains(@onclick,'clickAdd')]
    

    检查步骤:

    Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654329@匹配节点。

    如果我们有 1/1 匹配节点,请确保:

    1. 此 div 不在 iframe 下。
    2. 此 div 不在 shadow-root 下。
    3. 您不应该在 selenium 启动的新选项卡/窗口上。

    点击它,您可以使用以下代码试用:

    代码试用 1:

    time.sleep(5)
    driver.find_element_by_xpath("//input[@class='NewDelbuttoncss' and @id='Newbutton' and @value='New' and contains(@onclick,'clickAdd')]").click()
    

    代码试用 2:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='NewDelbuttoncss' and @id='Newbutton' and @value='New' and contains(@onclick,'clickAdd')]"))).click()
    

    代码试用 3:

    time.sleep(5)
    button = driver.find_element_by_xpath("//input[@class='NewDelbuttoncss' and @id='Newbutton' and @value='New' and contains(@onclick,'clickAdd')]")
    driver.execute_script("arguments[0].click();", button)
    

    代码试用 4:

    time.sleep(5)
    button = driver.find_element_by_xpath("//input[@class='NewDelbuttoncss' and @id='Newbutton' and @value='New' and contains(@onclick,'clickAdd')]")
    ActionChains(driver).move_to_element(button).click().perform()
    

    进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    

    【讨论】:

    • 非常感谢您的帮助,我可以看到 Chrome 正确指示并标记了 XPath,但它仍然没有按下您提供的选项的按钮。下面我附上所有的HTML。
    猜你喜欢
    • 2021-08-09
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2020-04-26
    • 2017-03-30
    • 2017-12-03
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多