2

I am trying to target a user in a list of other users. For example I created an automated test case to create a user named "tester1". I would now like to create a test case that finds and deletes "tester1" within a list of other users. Right now my work around is to find element by xpath which only targets a certain row within the user list. Here is the webpage code that I am working with:

<span class="delete" data-reactid=".0.1.1.0.2.1.0.$usertree.1.0.0.$tester1.0.0.1">
  <span class="icon-dash" data-reactid=".0.1.1.0.2.1.0.$usertree.1.0.0.$tester1.0.0.1.0"
  ></span>

Here is what I have been using for my workaround.

# Finds and clicks minus button to prompt deletion
# Deletes the 4th in the list / to change this, modify the number in the quotes below
element = driver.find_element_by_xpath("//tr[4]/td/div/span/span")
element.click()

# Finds and clicks Delete button
element = driver.find_element_by_xpath("//button[2]")
element.click()

EDIT:

<div class="tree-view_children" data-reactid=".0.1.1.0.2.1.0.$usertree.1">                          <table class="ss-table" data-reactide=".0.1.1.0.2.1.0.$usertree.1.0">
<tbody data-reactid=".0.1.1.0.2.1.0.$usertree.1.0.0">
<tr class="user-list-item" data-reactid=".0.1.1.0.2.1.0.$usertree.1.0.0.$tester1">
<td data-reactid=".0.1.1.0.2.1.0.$usertree.1.0.0.$tester1.0">
<div data-reactid=".0.1.1.0.2.1.0.$usertree.1.0.0.$tester1.0.0">
<span class="delete" data-reactid=".0.1.1.0.2.1.0.$usertree.1.0.0.$tester1.0.0.1">
<span class="icon-dash" data-    reactid=".0.1.1.0.2.1.0.$usertree.1.0.0.$tester1.0.0.1.0">
</span>
Chris
  • 123
  • 1
  • 1
  • 21

1 Answers1

1

It looks like you can rely on the tester's name being a part of data-reactid attribute:

tester = "tester1"

delete_button = driver.find_element_by_xpath("//span[@class = 'delete' and contains(@data-reactid, '$%s.')]" % tester)
delete_button.click()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    ^This is the right way to do this. An alternative to XPaths in this case is a CSS Selector, `span.delete[data-reactid*='tester1']` – JeffC Sep 11 '15 at 22:55
  • @alecxe I traded my lines of code with what you posted. Here are my results: "tester1" was located on the page but "delete_button.click()" did not click the delete button. At this point it gets to the user page finds "tester1" and does nothing. Any suggestions? – Chris Sep 14 '15 at 16:03
  • @JeffC I added your suggestion to my code and I got the same results that I posted above ^^^. – Chris Sep 14 '15 at 16:05
  • @Chris in this case, could you provide the complete HTML containing other "testers"? – alecxe Sep 14 '15 at 16:09
  • @Chris also, what browser are you using and are you sure an element is actually clickable? There is probably a button somewhere near the span we've just located..thanks. – alecxe Sep 14 '15 at 16:10
  • @ alecxe I added and "EDIT" section to the question above with extra HTML source code containing all of the HTML related with tester1. I am using firefox The button is clickable because when I run my workaround code it clicks it. The page is a list of users with a minus sign next to the user name that if you click it will bring up a little window asking if you want to delete user. – Chris Sep 14 '15 at 16:55