Python硒右键单击

时间:2018-07-31 18:00:35

标签: python python-3.x selenium selenium-webdriver contextmenu

尝试查找一些文本,然后右键单击该文本。该表是动态的,因此我无法假定知道要搜索的值或单击值的单元格位置。例如,我想找到一个名称,然后右键单击名称本身

右键单击菜单似乎称为 class =上下文菜单,其值为 class = context_item (请参阅随附的屏幕截图)(还包括html)

我知道这部分有效,但是它下面的其余部分都有问题-col = row.find_element(By.XPATH, '//a[contains(text() , "Luke Wilson")]')

###输出
  

回溯(最近通话最近):文件   “ C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ ffox_Change_Create_Save_Stay_v2.py”,   182行,在       get_all_rows_approval()文件“ C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ ffox_Change_Create_Save_Stay_v2.py”,   第164行,在get_all_rows_approval中       菜单= col.find_element_by_class_name(“ context_menu”)文件“ C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webelement.py”,   第398行,在find_element_by_class_name中       返回self.find_element(by = By.CLASS_NAME,值=名称)文件“ C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webelement.py”,   第654行,在find_element中       {“使用”:通过,“值”:值})['值']文件“ C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webelement。 py”,   _execute中的第628行       返回self._parent.execute(命令,参数)文件“ C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,   第320行,执行       self.error_handler.check_response(响应)文件“ C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py”,   第242行,在check_response中       引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchElementException:消息:无法   定位元素:.context_menu

代码段

def get_all_rows_approval():

    approval_table = driver.find_element(By.XPATH, '//*[@id="change_request.sysapproval_approver.sysapproval_table"]/tbody')
    rows = approval_table.find_elements(By.XPATH, "//*[contains(@id,'row_change_request.sysapproval_approver.sysapproval')]")
    for row in rows:
        col = row.find_element(By.XPATH, '//a[contains(text() , "Luke Wilson")]')
        menu = col.find_element_by_class_name("context_menu")
        actions = ActionChains(menu)
        actions.move_to_element(menu)
        actions.click(menu)
        actions.perform()
表的#### html
                     <tbody class="list2_body">
                        <tr id="row_change_request.sysapproval_approver.sysapproval_30f168b2dbeb5300b58ad360cf961998" class="list_row list_odd" style="" sys_id="30f168b2dbeb5300b58ad360cf961998" record_class="sysapproval_approver" data-updated-on="2018-07-30 13:36:23" collapsed="true" data-type="list2_row" data-list_id="change_request.sysapproval_approver.sysapproval">
                           <td class="list_decoration_cell col-control col-small col-center " style="white-space:nowrap;" rowspan="1"><span class="input-group-checkbox"><input title="Mark record for List Action" id="check_change_request.sysapproval_approver.sysapproval_30f168b2dbeb5300b58ad360cf961998" name="check_change_request.sysapproval_approver.sysapproval" class="checkbox " data-type="list2_checkbox" data-list_id="change_request.sysapproval_approver.sysapproval" data-original-title="Mark record for List Action" type="checkbox"><label for="check_change_request.sysapproval_approver.sysapproval_30f168b2dbeb5300b58ad360cf961998" style="" class="checkbox-label"><span class="sr-only">Select record for action</span></label></span></td>
                           <td class="list_decoration_cell col-small col-center " rowspan="1"><a href="sysapproval_approver.do?sys_id=30f168b2dbeb5300b58ad360cf961998&amp;sysparm_view=&amp;sysparm_record_target=task_ci&amp;sysparm_record_row=1&amp;sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder&amp;sysparm_record_rows=5" class="btn btn-icon table-btn-lg icon-info list_popup" data-type="list2_popup" data-list_id="change_request.sysapproval_approver.sysapproval" style="margin-left:0px"><span class="sr-only">Preview</span></a></td>
                           <td class="vt" ng-non-bindable="">
                              <span class="sr-only"></span>
                              <div class="list2_cell_background" style="background-color: khaki"></div>
                              <a class="linked formlink" href="sysapproval_approver.do?sys_id=30f168b2dbeb5300b58ad360cf961998&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=1&amp;sysparm_record_rows=5&amp;sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder">Requested</a>
                           </td>
                           <td class="vt" ng-non-bindable=""><a class="linked" sys_id="97000fcc0a0a0a6e0104ca999f619e5b" href="sys_user.do?sys_id=97000fcc0a0a0a6e0104ca999f619e5b">Christen Mitchell</a></td>
                           <td class="vt" ng-non-bindable=""><a class="linked" sys_id="b85d44954a3623120004689b2d5dd60a" href="sys_user_group.do?sys_id=b85d44954a3623120004689b2d5dd60a">CAB Approval</a></td>
                           <td class="vt" ng-non-bindable=""></td>
                           <td class="vt" ng-non-bindable="">
                              <div class="datex date-calendar" title="" timeago="2018-07-30 13:36:23" timeago-attrs="title" data-original-title="5m ago">2018-07-30 06:36:23</div>
                              <div class="datex date-calendar-short" title="just now" timeago="2018-07-30 13:36:23" timeago-attrs="title" data-original-title="5m ago">07-30 06:36</div>
                              <div class="datex date-timeago" title="2018-07-30 06:36:23" timeago="2018-07-30 13:36:23" data-original-title="2018-07-30 06:36:23" null="5m ago">5m ago</div>
                           </td>
                           <td class="vt vt-spacer" style="padding: 0"></td>
                        </tr>
                        <tr id="row_change_request.sysapproval_approver.sysapproval_34f168b2dbeb5300b58ad360cf961997" class="list_row list_even" style="" sys_id="34f168b2dbeb5300b58ad360cf961997" record_class="sysapproval_approver" data-updated-on="2018-07-30 13:36:23" collapsed="true" data-type="list2_row" data-list_id="change_request.sysapproval_approver.sysapproval">
                           <td class="list_decoration_cell col-control col-small col-center " style="white-space:nowrap;" rowspan="1"><span class="input-group-checkbox"><input title="Mark record for List Action" id="check_change_request.sysapproval_approver.sysapproval_34f168b2dbeb5300b58ad360cf961997" name="check_change_request.sysapproval_approver.sysapproval" class="checkbox " data-type="list2_checkbox" data-list_id="change_request.sysapproval_approver.sysapproval" data-original-title="Mark record for List Action" type="checkbox"><label for="check_change_request.sysapproval_approver.sysapproval_34f168b2dbeb5300b58ad360cf961997" style="" class="checkbox-label"><span class="sr-only">Select record for action</span></label></span></td>
                           <td class="list_decoration_cell col-small col-center " rowspan="1"><a href="sysapproval_approver.do?sys_id=34f168b2dbeb5300b58ad360cf961997&amp;sysparm_view=&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=2&amp;sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder&amp;sysparm_record_rows=5" class="btn btn-icon table-btn-lg icon-info list_popup" data-type="list2_popup" data-list_id="change_request.sysapproval_approver.sysapproval" style="margin-left:0px"><span class="sr-only">Preview</span></a></td>
                           <td class="vt" ng-non-bindable="">
                              <span class="sr-only"></span>
                              <div class="list2_cell_background" style="background-color: khaki"></div>
                              <a class="linked formlink" href="sysapproval_approver.do?sys_id=34f168b2dbeb5300b58ad360cf961997&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=2&amp;sysparm_record_rows=5&amp;sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder">Requested</a>
                           </td>
                           <td class="vt" ng-non-bindable=""><a class="linked" sys_id="46d96f57a9fe198101947a9620895886" href="sys_user.do?sys_id=46d96f57a9fe198101947a9620895886">Luke Wilson</a></td>
                           <td class="vt" ng-non-bindable=""><a class="linked" sys_id="b85d44954a3623120004689b2d5dd60a" href="sys_user_group.do?sys_id=b85d44954a3623120004689b2d5dd60a">CAB Approval</a></td>
                           <td class="vt" ng-non-bindable=""></td>
                           <td class="vt" ng-non-bindable="">
                              <div class="datex date-calendar" title="" timeago="2018-07-30 13:36:23" timeago-attrs="title" data-original-title="5m ago">2018-07-30 06:36:23</div>
                              <div class="datex date-calendar-short" title="just now" timeago="2018-07-30 13:36:23" timeago-attrs="title" data-original-title="5m ago">07-30 06:36</div>
                              <div class="datex date-timeago" title="2018-07-30 06:36:23" timeago="2018-07-30 13:36:23" data-original-title="2018-07-30 06:36:23" null="5m ago">5m ago</div>
                           </td>
                           <td class="vt vt-spacer" style="padding: 0"></td>
                        </tr>
                        <tr id="row_change_request.sysapproval_approver.sysapproval_b4f168b2dbeb5300b58ad360cf961998" class="list_row list_odd" style="" sys_id="b4f168b2dbeb5300b58ad360cf961998" record_class="sysapproval_approver" data-updated-on="2018-07-30 13:36:23" collapsed="true" data-type="list2_row" data-list_id="change_request.sysapproval_approver.sysapproval">
                           <td class="list_decoration_cell col-control col-small col-center " style="white-space:nowrap;" rowspan="1"><span class="input-group-checkbox"><input title="Mark record for List Action" id="check_change_request.sysapproval_approver.sysapproval_b4f168b2dbeb5300b58ad360cf961998" name="check_change_request.sysapproval_approver.sysapproval" class="checkbox " data-type="list2_checkbox" data-list_id="change_request.sysapproval_approver.sysapproval" data-original-title="Mark record for List Action" type="checkbox"><label for="check_change_request.sysapproval_approver.sysapproval_b4f168b2dbeb5300b58ad360cf961998" style="" class="checkbox-label"><span class="sr-only">Select record for action</span></label></span></td>
                           <td class="list_decoration_cell col-small col-center " rowspan="1"><a href="sysapproval_approver.do?sys_id=b4f168b2dbeb5300b58ad360cf961998&amp;sysparm_view=&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=3&amp;sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder&amp;sysparm_record_rows=5" class="btn btn-icon table-btn-lg icon-info list_popup" data-type="list2_popup" data-list_id="change_request.sysapproval_approver.sysapproval" style="margin-left:0px"><span class="sr-only">Preview</span></a></td>
                           <td class="vt" ng-non-bindable="">
                              <span class="sr-only"></span>
                              <div class="list2_cell_background" style="background-color: khaki"></div>
                              <a class="linked formlink" href="sysapproval_approver.do?sys_id=b4f168b2dbeb5300b58ad360cf961998&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=3&amp;sysparm_record_rows=5&amp;sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder">Requested</a>
                           </td>
                           <td class="vt" ng-non-bindable=""><a class="linked" sys_id="ee826bf03710200044e0bfc8bcbe5de6" href="sys_user.do?sys_id=ee826bf03710200044e0bfc8bcbe5de6">Bernard Laboy</a></td>
                           <td class="vt" ng-non-bindable=""><a class="linked" sys_id="b85d44954a3623120004689b2d5dd60a" href="sys_user_group.do?sys_id=b85d44954a3623120004689b2d5dd60a">CAB Approval</a></td>
                           <td class="vt" ng-non-bindable=""></td>
                           <td class="vt" ng-non-bindable="">
                              <div class="datex date-calendar" title="" timeago="2018-07-30 13:36:23" timeago-attrs="title" data-original-title="5m ago">2018-07-30 06:36:23</div>
                              <div class="datex date-calendar-short" title="just now" timeago="2018-07-30 13:36:23" timeago-attrs="title" data-original-title="5m ago">07-30 06:36</div>
                              <div class="datex date-timeago" title="2018-07-30 06:36:23" timeago="2018-07-30 13:36:23" data-original-title="2018-07-30 06:36:23" null="5m ago">5m ago</div>
                           </td>
                           <td class="vt vt-spacer" style="padding: 0"></td>
                        </tr>
                        <tr id="row_change_request.sysapproval_approver.sysapproval_b8f168b2dbeb5300b58ad360cf961997" class="list_row list_even" style="" sys_id="b8f168b2dbeb5300b58ad360cf961997" record_class="sysapproval_approver" data-updated-on="2018-07-30 13:36:23" collapsed="true" data-type="list2_row" data-list_id="change_request.sysapproval_approver.sysapproval">
                           <td class="list_decoration_cell col-control col-small col-center " style="white-space:nowrap;" rowspan="1"><span class="input-group-checkbox"><input title="Mark record for List Action" id="check_change_request.sysapproval_approver.sysapproval_b8f168b2dbeb5300b58ad360cf961997" name="check_change_request.sysapproval_approver.sysapproval" class="checkbox " data-type="list2_checkbox" data-list_id="change_request.sysapproval_approver.sysapproval" data-original-title="Mark record for List Action" type="checkbox"><label for="check_change_request.sysapproval_approver.sysapproval_b8f168b2dbeb5300b58ad360cf961997" style="" class="checkbox-label"><span class="sr-only">Select record for action</span></label></span></td>
                           <td class="list_decoration_cell col-small col-center " rowspan="1"><a href="sysapproval_approver.do?sys_id=b8f168b2dbeb5300b58ad360cf961997&amp;sysparm_view=&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=4&amp;sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder&amp;sysparm_record_rows=5" class="btn btn-icon table-btn-lg icon-info list_popup" data-type="list2_popup" data-list_id="change_request.sysapproval_approver.sysapproval" style="margin-left:0px"><span class="sr-only">Preview</span></a></td>
                           <td class="vt" ng-non-bindable="">
                              <span class="sr-only"></span>
                              <div class="list2_cell_background" style="background-color: khaki"></div>
                              <a class="linked formlink" href="sysapproval_approver.do?sys_id=b8f168b2dbeb5300b58ad360cf961997&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=4&amp;sysparm_record_rows=5&amp;sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder">Requested</a>
                           </td>
                           <td class="vt" ng-non-bindable=""><a class="linked" sys_id="62d78687c0a8010e00b3d84178adc913" href="sys_user.do?sys_id=62d78687c0a8010e00b3d84178adc913">Ron Kettering</a></td>
                           <td class="vt" ng-non-bindable=""><a class="linked" sys_id="b85d44954a3623120004689b2d5dd60a" href="sys_user_group.do?sys_id=b85d44954a3623120004689b2d5dd60a">CAB Approval</a></td>
                           <td class="vt" ng-non-bindable=""></td>
                           <td class="vt" ng-non-bindable="">
                              <div class="datex date-calendar" title="just now" timeago="2018-07-30 13:36:23" timeago-attrs="title" data-original-title="5m ago">2018-07-30 06:36:23</div>
                              <div class="datex date-calendar-short" title="just now" timeago="2018-07-30 13:36:23" timeago-attrs="title" data-original-title="5m ago">07-30 06:36</div>
                              <div class="datex date-timeago" title="2018-07-30 06:36:23" timeago="2018-07-30 13:36:23" data-original-title="2018-07-30 06:36:23" null="5m ago">5m ago</div>
                           </td>
                           <td class="vt vt-spacer" style="padding: 0"></td>
                        </tr>

                     </tbody>
                  </table>
                  <a id="change_request.sysapproval_approver.sysapproval_bottom" href="javascript:function(evt){if(evt){evt.stopPropagation();}}"><span class="sr-only">Bottom of table</span></a>
               </div>
            </td>
         </tr>
      </tbody>
   </table>
</html>

1 个答案:

答案 0 :(得分:2)

请注意,click()不会右键单击,而是context_click()

如果要基于名称右键单击,则可以使用以下代码:

ActionChains(driver).context_click(driver.find_element_by_xpath("//a[text()='Luke Wilson']").perform()  

您也可以尝试以下方法:

action = ActionChains(driver)
action.move_to_element(driver.find_element_by_xpath("//a[text()='Luke Wilson']")).perform();
action.context_click().perform()