Trying to find some text and then right click on that text. The table is dynamic so I cannot presume to know the cell positions of where the value to search for is or the value to click on. so for example I want to find a name and then right click on the name itself
The right click menu seems to be called class = context menu, and the values are class = context_item (See attached screenshots) (html included also)
I know that this part works, but the rest below it is having an issue - col = row.find_element(By.XPATH, '//a[contains(text() , "Luke Wilson")]')
#Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python36\ffox_Change_Create_Save_Stay_v2.py", line 182, in get_all_rows_approval() File "C:\Users\AppData\Local\Programs\Python\Python36\ffox_Change_Create_Save_Stay_v2.py", line 164, in get_all_rows_approval menu = col.find_element_by_class_name("context_menu") File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 398, in find_element_by_class_name return self.find_element(by=By.CLASS_NAME, value=name) File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 654, in find_element {"using": by, "value": value})['value'] File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute return self._parent.execute(command, params) File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute self.error_handler.check_response(response) File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .context_menu
Code snippet
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 for the table
<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&sysparm_view=&sysparm_record_target=task_ci&sysparm_record_row=1&sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder&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&sysparm_record_target=sysapproval_approver&sysparm_record_row=1&sysparm_record_rows=5&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&sysparm_view=&sysparm_record_target=sysapproval_approver&sysparm_record_row=2&sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder&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&sysparm_record_target=sysapproval_approver&sysparm_record_row=2&sysparm_record_rows=5&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&sysparm_view=&sysparm_record_target=sysapproval_approver&sysparm_record_row=3&sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder&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&sysparm_record_target=sysapproval_approver&sysparm_record_row=3&sysparm_record_rows=5&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&sysparm_view=&sysparm_record_target=sysapproval_approver&sysparm_record_row=4&sysparm_record_list=sysapproval%3De6d1ac72dbeb5300b58ad360cf96193e%5EORDERBYorder&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&sysparm_record_target=sysapproval_approver&sysparm_record_row=4&sysparm_record_rows=5&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>
We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.
Selenium uses the Actions class to perform the right click action. The contextClick() is a method under Actions class to do the right click and once the menu opens, we can select an option from them via automation.
This article revolves around double_click method on Action Chains in Python Selenium. double_click method is used to double click on an element or current position. on_element – The element to click. If None, clicks on current mouse position.
For automating the right-click operation, Selenium provides a dedicated method – contextClick(). This method accepts the target WebElement as the argument. In order to use this method, use the Actions class object. The method of locating the desired element remains the same.
Note that click()
will not do right click, it is context_click()
If you want to right click on based on name, then you can use this code :
ActionChains(driver).context_click(driver.find_element_by_xpath("//a[text()='Luke Wilson']").perform()
You can try with this also :
action = ActionChains(driver)
action.move_to_element(driver.find_element_by_xpath("//a[text()='Luke Wilson']")).perform();
action.context_click().perform()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With