Constant click using Selenium does not work

Asked

Viewed 101 times

0

Goal: Click 4 times on the same button "See More"

Problem: Click command works the first 2 times, after that, it no longer works and returns TimeoutException

Code:

ops = webdriver.ChromeOptions()
ops.add_argument("start-maximized")
ops.add_argument('disable-infobars')

driver = webdriver.Chrome(options=ops, executable_path='C:\\Users\\danie\\AppData\\Local\\Programs\\Python\\Python37\\chromedriver.exe')

driver.get('https://busca.estadao.com.br/?tipo_conteudo=Not%C3%ADcias&quando=01%2F08%2F2018-01%2F11%2F2018&q=Jo%C3%A3o%20D%C3%B3ria')

for i in range(4):
    try:
        #"btn-mais" é a classe do botão que quero clicar
        driver.execute_script("arguments[0].click();", WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'btn-mais'))))
        time.sleep(5)
    except:
        break

1 answer

1


The problem is that when you click on "load more" the page makes an ajax request and joins the server response to div main, and the button you clicked on initially gets hidden and the driver can’t find it (that’s why you get the TimeoutException.

Changing the way searches by clicking button solves the problem:

driver.execute_script("arguments[0].click();",\
WebDriverWait(driver,20).\
until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".mais-itens:last-child > div > a"))))

Browser other questions tagged

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