2

Here is an interesting problem (or at least i think so) As an Admin I have a table with N entries (51 to give a number). Each entry in a row is a recruiter name with a link to 'Login' as that person. I want to be able to click on each recruiter 'login' which takes me to that account, verify some facts there, logout, Relogin as the admin and continue the cylcle (login as recruiter,verify,logout etc) I find the elements in the table by finding each row and then each cell. (find_elements_by_tag_name("tr") followed by find_elements_by_tag_name("td")).

The problem arises when I Logout as the recruiter and ReLogin as the admin, now the cells that I had found earlier throw a stale element exceptions.

for row in rows: 
 if chk == "true":
 cells[6].find_element_by_link_text("Login").click() # recruiter login
 alert = driver.switch_to_alert()
 alert.accept()
 #Verify Recruiter account is the same as the one you logged in as
 driver.find_element_by_link_text("Logout").click()
 self.login() #admin login
 self.bringUpRecruiters()

I could call the function which finds the rows and columns at each iteration. However, it takes half a minute for webdriver to find all the cell elements (51 rows x 7 columns) at each iteration. For 51 iterations that is approximately 25 mins. And if the number of rows increase it will only increase more.

Is there a more elegant and time saving way of doing this ? I would appreciate your bright ideas!

I am using Webdriver + Python bindings.

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
asked May 29, 2012 at 22:49
2
  • @user141110 Welcome to SQA. Let me make sure I understand. In a 51 x 7 table, how many login links are there: 51 or 357? Commented May 30, 2012 at 2:27
  • Thanks 246! There are 51 login links (non unique. so i cannot save the links in one iteration and then circle through them).There are 51 rows each row with 7 columns. I need to check if condition in column 1 is true then click on link in column 7. Commented May 30, 2012 at 17:16

1 Answer 1

3

I wonder if it would be faster to use Xpaths to search specifically for cells in the row of interest, rather than fetching all rows and then manipulating the one row you are interested in. I am not fluent in Python, so I will illustrate my suggestion in pseudo-code:

for i in 1..51
 condition_xpath = "//table/tr[" + i + "]/td[1]"
 column1 = driver.find_element_by_xpath(condition_xpath)
 if condition in column 1 is true
 login_xpath = "//table/tr[" + i + "]/td[7]/a"
 driver.find_element_by_xpath(login_xpath).click()
 alert = driver.switch_to_alert()
 alert.accept()
 #Verify Recruiter account is the same as the one you logged in as
 driver.find_element_by_link_text("Logout").click()
 self.login() #admin login
 self.bringUpRecruiters()
 end if
end for
answered May 30, 2012 at 18:22
4
  • I am going to try this now. looks like this will work.Thanks! Commented May 30, 2012 at 18:51
  • Good answer. An alternative that avoids using xpath (which can be slow) to avoid the stale element exception (and to get a little closer to following the page object pattern), you need to create a new instance of the element each time the page is re-loaded. You can create a function called ClickLoginForUser that takes an integer for the index. In that function you would create all of the elements and interact with them so that they don't get stale. You would then call your function inside the for loop. Commented May 30, 2012 at 22:00
  • @Sam Woods What does "create all of the elements" translate to in terms of Selenium locators? Commented May 30, 2012 at 22:45
  • Execute the find_element_by function to set your element object. Commented May 31, 2012 at 17:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.