简体   繁体   English

单击表格中的特定文本

[英]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 总是在我注册用户时,它显示在最后一页的表格中(当前我有4页注册用户),因此我需要转到最后一页并单击创建的该用户

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. 除了使用for循环和while-else ,还可以使用while循环单击Next链接(只要存在)。 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. 您应该能够构建定位器以找到所需的确切元素,但是您需要提供相关的HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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