Selenium (list of elements of a class)

Asked

Viewed 563 times

1

Greetings !

I want to open all the elements (not repeated) of a class, each in a new tab, how do I do it ? My attempt:

lista_elementos = driver.find_elements_by_class_name('card-action')
for i in lista_elementos:
    if i not in acessados:
        acessados.append(i)
        i.send_keys(Keys.CONTROL + 't')
    else:
        break

Another attempt, but this element "i" replaces the main tab I’m using, and that’s not what interests me

lista_elementos = driver.find_elements_by_class_name('card-action')
    for i in lista_elementos:
        if i not in acessados:
            acessados.append(i)
            i.click()
        else:
            break

2 answers

0

You can access the attribute href of each element of the class and open this url in a new tab. So:

lista_elementos = driver.find_elements_by_class_name('card-action')
for i in lista_elementos:
    if i not in acessados:
        acessados.append(i)
        url = i.get_attribute('href')
        driver.execute_script("window.open('"+url+"', '_blank')")

    else:
        break

And to change tab:

driver.switch_to_window(driver.window_handles[1])

0

Thanks for the answer, but he returned all the elements as "None"

lista_elementos = driver.find_elements_by_class_name('card-action')
# ABRINDO TODAS AS DISCIPLINAS
for i in lista_elementos:
    if i not in acessados:
        tag_a = driver.find_element_by_tag_name('a').get_attribute('href')
        print(tag_a)
    else:
        break

I saw some examples and came to this code, but the problem now is that it returns a default link, instead of the link of each element:

Todos os elementos retornando um link padrão

Instead of taking, for example, a link of this type: inserir a descrição da imagem aqui

Would that be some kind of HTML protection ? Or am I doing something wrong ?

Browser other questions tagged

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