简体   繁体   中英

Click on a specific text in a table

Always when I register a user, It's shown in a table on the last page (currently I have 4 pages of registered users), so I need to go to the last page and click this user created

My code:

Locators used

class FeeScheduleLocators(object):

  ADD_BUTTON = (By.ID, "save")
  NAME = (By.ID, "id_name")
  SAVE_BUTTON = (By.CSS_SELECTOR, "input[type='submit']")
  FEE_SCHEDULE_LIST = (By.TAG_NAME, "td")
  NEXT_LINK = (By.LINK_TEXT, "Next")

Method used

def create_fee_schedule(self):

    self.name = fake.name()

    self.find_element(FeeScheduleLocators.NAME).send_keys(self.name)

    self.find_element(FeeScheduleLocators.SAVE_BUTTON).click()
    WebDriverWait(self.driver, AUTOCOMPLETE_TIMEOUT).until(
        EC.text_to_be_present_in_element((By.CLASS_NAME, "success"), 'Fee added sucessfully'))

    self.fee_list = self.find_elements(FeeScheduleLocators.FEE_SCHEDULE_LIST)

    for self.each_register in self.fee_list:

        while self.each_register.text != self.name:

            self.find_element(FeeScheduleLocators.NEXT_LINK).click()
            time.sleep(2)

        else:

            each_register.click()

My problem after run

The test click just to the second page and shows the error below:

E   StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
E     (Session info: chrome=56.0.2924.87)
E     (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Mac OS X 10.12.3 x86_64)

Expected behavior:

go to the last page and click the name of the user created recently

From your description, the desired name should always be on the last page. Your code is looking for the desired name before you've reached the last page.

Instead of using a for loop and while-else , use a while loop to click the Next link as long as it exists. Once it no longer exists, you know that you are on the last page. Then click on the last name or find the desired element directly and click it. You should be able to craft a locator to find the exact element you want but you would need to provide the relevant HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM