Run Selenium Event

Asked

Viewed 155 times

0

I have the following problem when trying to click on a pagination using python and Selenium

inserir a descrição da imagem aqui

Paging is a table

<td class="dr-dscr-inact rich-datascr-inact" onclick="Event.fire(this, 'rich:datascroller:onscroll', {'page': '2});">2</td>

created the following method

def Proxima_pagina():
#pego o id da tabela onde está a paginação
    tabela = Driver.find_element_by_id('form:j_id162:dtRick_table')

#faço um for em todos os td e mando clicar
    for td in tabela.find_elements_by_xpath(".//td[@class='dr-dscr-inact rich-datascr-inact']"):

        td.click()

it starts at the first click on 2, but gives the following error

inserir a descrição da imagem aqui

2 answers

0

So friend, it works like this, this error concerns the element reference, you have the element reference but he is not on the screen.

You will find it strange because this seeing the element on the screen, but the reference is to the old object, as soon as you clicked on the next page, the table on the screen is no longer the same, so your reference is 'outdated'.

How to solve:

Take the current page, and then look for the next one, if you have it, click. If you, take the total at the moment, it may be that after the 9 have more pages and you would end up not considering them.

Example:

def proxima_pagina():
   # Pega a página atual, a pagina selecionada no momento deve ter algum atributo único.
   current_page = Driver.find_element_by_xpath('XPATH_DA_PAGINA_DO_MOMENTO')

   # Procura pela próxima e clica, caso não encontre vai parar e mostrar a mensagem.
   try:
       next_page = current_page.find_element_by_xpath('/following-sibling::td[1]')
       next_page.click()
   except NoSuchElementException:
       print('No more pages')

You’ll need this import here:

from selenium.common.exceptions import NoSuchElementException

And if you want to leave by clicking on all the pages, you make a for calling the method.

  • Thanks for the answer. The page seems to me if loaded by Ajax.

  • Still in error? What was the result?

0

Can solve with the following code.

def Total_pagina():
    time.sleep(3)
    tabela = Driver.find_elements_by_xpath(".//td[@class='dr-dscr-inact rich-datascr-inact']")
    total = len(tabela)
    return total + 1

def Proxima_pagina():
    for A in range(1, Total_pagina()):
        thisEl = Driver.find_element_by_xpath(
            '//td[contains(@class, "rich-datascr-inact") and contains(text(), "{0}")]'.format(str(A + 1)))
        time.sleep(6)
        baixar()
        thisEl.click()
        time.sleep(6)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.