Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

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'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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() 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...