jack_boardman94
jack_boardman94

Reputation: 11

Clicking multiple links with the same link text using python selenium

I'm trying to get my program (written in python) to open multiple links at the same time. I can open one link – but when I change my command from find_element_by_link_text to find_elements_by_link_text it doesn't work.

For context, I'm trying to open all links on the page with the name 'Annual Report'.

Here's the code I've tried:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import options



driver = webdriver.Chrome()
driver.get('https://webb-site.com/')

search = driver.find_element_by_name('code')

search.click()

search.send_keys("1830")

search.send_keys(Keys.RETURN)


element = driver.find_element_by_link_text('Financials')

element.click()

    
element = driver.find_elements_by_link_text('Annual Report')

element.click()

Here's my error

AttributeError: 'list' object has no attribute 'click'

As I said above. By removing the s in 'elements' it works fine but only opens the first 'Annual Report'.

Upvotes: 1

Views: 2672

Answers (2)

Moshe Slavin
Moshe Slavin

Reputation: 5204

You are calling click to a list object.

The object element in the last line:

element = driver.find_elements_by_link_text('Annual Report')

is a list.

When you call find_elements you get in return a list.

Just change it to:

element = driver.find_element_by_link_text('Annual Report')

Edit

As @Samsul added, to click on all the elements you'll need to loop through the list.

Upvotes: 2

Samsul Islam
Samsul Islam

Reputation: 2609

You are getting AttributeError: 'list' object has no attribute 'click' error due to click on a list object. If you want to click multiple items then use a for loop.

driver = webdriver.Chrome()
driver.get('https://webb-site.com/')

search = driver.find_element_by_name('code')

search.click()

search.send_keys("1830")

search.send_keys(Keys.RETURN)


element = driver.find_element_by_link_text('Financials')

element.click()


element = driver.find_elements_by_link_text('Annual Report')
# print(element)
# print(len(element))
# element.click()
for c in element:
    c.click() 

Upvotes: 4

Related Questions