Python & Selenium Clicking on td value in a row based on another td in the same row

橙三吉。 提交于 2021-01-29 06:01:13

问题


I'm trying to find out how to right click on a td based on a value in another td. Starting with the code below was just trying to find an initial cell and click on that value. If I could get that to work I wanted to right click on a value in the same row that has the text "Requested". The table is dynamic so the columns could be in any order. So for example. Find the name "Howard Johnson" and then right click on the requested value in the state column. (see attached image)

The output is - Traceback (most recent call last):

  File "C:\Users\hassong\Documents\Automation\ffox_Change_Create_Save_Stay_Modified.py", line 135, in <module>
    get_all_rows_approval()
  File "C:\Users\hassong\Documents\Automation\ffox_Change_Create_Save_Stay_Modified.py", line 127, in get_all_rows_approval
    cols = row.find_element(By.XPATH, "//tr/td[contains(text(), 'Howard Johnson')]")
  File "C:\Users\hassong\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\hassong\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\hassong\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\hassong\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: //tr/td[contains(text(), 'Howard Johnson')]

Table html snippet

enter image description here

## Program
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains

base_url =  "https://xxxx.service-now.com/login.do"

driver = webdriver.Firefox()
driver.implicitly_wait(10)

# CHG CREATE WITH SAVE AND STAY

driver.get(base_url)
username = driver.find_element_by_id("user_name")
username.send_keys("xxx")





time.sleep(1)


password = driver.find_element_by_id("user_password")
password.send_keys("xxxx")

time.sleep(1)

loginBtn = driver.find_element_by_id("sysverb_login")
loginBtn.click()

time.sleep(1)
#driver.implicitly_wait(5)

#driver = window.maximize()
createnew = driver.find_element_by_id("filter")
createnew.send_keys("Change")
time.sleep(2)
createnew.send_keys(Keys.RETURN)

driver.switch_to_frame("gsft_main")
time.sleep(1)

normal_chg = driver.find_element(By.XPATH, '/html/body/form/div[2]/a')
normal_chg.click()

# Create new normal change information
# # Top screen left
select = Select(driver.find_element_by_id("change_request.category"))
select.select_by_visible_text("Hardware")
time.sleep(1)

assigned_to = driver.find_element_by_name("sys_display.change_request.cmdb_ci")
assigned_to.send_keys("lnux100")
assigned_to.send_keys(Keys.RETURN)
time.sleep(1)


select = Select(driver.find_element_by_id("change_request.priority"))
select.select_by_visible_text("2 - High")
time.sleep(1)

select = Select(driver.find_element_by_id("change_request.risk"))
select.select_by_visible_text("High")
time.sleep(1)

select = Select(driver.find_element_by_id("change_request.impact"))
select.select_by_visible_text("1 - High")
time.sleep(1)

# Top screen right

assigned_to = driver.find_element_by_name("sys_display.change_request.assignment_group")
assigned_to.send_keys("CAB Approval")
assigned_to.send_keys(Keys.RETURN)

#Middle

short_desc = driver.find_element_by_id("change_request.short_description")
short_desc.send_keys("This is an automated test...")
time.sleep(1)

desc = driver.find_element_by_id("change_request.description")
desc.send_keys("Testing Notes")
time.sleep(1)



# Now save with basic information
button = driver.find_element(By.XPATH, '/html/body/div[1]/span/span/nav/div/div[1]/button[2]')
button.click()
save_btn = driver.find_element(By.XPATH, '/html/body/div[7]/div[2]')
save_btn.click()
#button.send_keys(Keys.RETURN)
time.sleep(4)


# Request Approval
request_app_btn = driver.find_element(By.XPATH, '//*[@id="state_model_request_assess_approval"]')
request_app_btn.click()
time.sleep(4)

# scroll down
element = driver.find_element(By.XPATH, '//*[@id="tabs2_list"]/span[3]/span/span[2]')
element.location_once_scrolled_into_view

# Approval Tab - Scroll To
element = driver.find_element(By.XPATH, '//*[@id="tabs2_list"]/span[3]/span/span[2]')
actions = ActionChains(driver)
actions.move_to_element(element).perform()
time.sleep(3)


#Click on approvers tab
approval_tab = driver.find_element(By.XPATH, '//*[@id="tabs2_list"]/span[3]/span/span[2]')
approval_tab.click()
time.sleep(3)

def get_all_rows_approval():
    approval_table = driver.find_element(By.XPATH, '//*[@id="change_request.sysapproval_approver.sysapproval_table"]/tbody')

    # get all rows in the table
    rows = approval_table.find_elements_by_tag_name("tr")
#    rows = approval_table.find_elements(By.XPATH, "//*[contains(@id,'row_change_request.sysapproval_approver.sysapproval')]")
    for row in rows:
        cols = row.find_element(By.XPATH, "//tr/td[contains(text(), 'Howard Johnson')]")
        cols.click()

get_all_rows_approval()

<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>
<tr id="row_change_request.sysapproval_approver.sysapproval_bcf168b2dbeb5300b58ad360cf961996" class="list_row list_odd" style="" sys_id="bcf168b2dbeb5300b58ad360cf961996" 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_bcf168b2dbeb5300b58ad360cf961996" 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_bcf168b2dbeb5300b58ad360cf961996" 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=bcf168b2dbeb5300b58ad360cf961996&amp;sysparm_view=&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=5&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=bcf168b2dbeb5300b58ad360cf961996&amp;sysparm_record_target=sysapproval_approver&amp;sysparm_record_row=5&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="46ca0887a9fe19810191e08e51927ebf" href="sys_user.do?sys_id=46ca0887a9fe19810191e08e51927ebf">Howard Johnson</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>
 </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>

回答1:


You can use this xpath :

//a[text()='Luke Wilson']/parent::td/preceding-sibling::td[@class='vt']/a  

Note that based on name Luke Wilson , it will give you requested hyperlink which you can use to right click.

In case you want that name to be picked by script dynamically :

name = Luke Wilson
//a[text()='"+name+"']/parent::td/preceding-sibling::td[@class='vt']/a

Hope that helps.



来源:https://stackoverflow.com/questions/51595563/python-selenium-clicking-on-td-value-in-a-row-based-on-another-td-in-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!