1

What is the updated way to write find elements by link text? long story longer... I have about 4000 links on various pages of a website to click. (If I can automate this I can use it as a template for the rest of the common link texts as well on other pages)

specifically, I need to know where I went wrong? Been at this for some time now. I decided to throw in the towel for the night but I cannot find a workaround that is liked my mac terminal/visual studio/ pylance. they all love to yell lol. anyway, I am on a MacBook Pro Laptop and I'd really appreciate some help!

import webbrowser
import time
from selenium import webdriver

driver = webdriver.Chrome

# Not an actual link for public purposes
URL = "https://banfaouf.com/lfmamdofpisao"
webbrowser.open(URL)

# Find all links that contain specific text (e.g., 'Click Me' in this example)
links = driver.find_elements_by_link_text('Download File')


# Add a time delay (in seconds) between clicks
time_delay_between_clicks = 2  # Adjust this value as needed (e.g., 2 seconds)

# Click each link with a time delay between clicks
for link in links:
    link.click()
    time.sleep(time_delay_between_clicks)```

(This is my first post on this platform!) Idk if this makes any difference but among factors to consider: I' using: MacBook Pro (all up to date) and Mac Terminal is also in working order Latest Version of Python

  • Selenium
  • VS Code
  • chrome driver

have been installed

eng
  • 443
  • 1
  • 4
  • 10

1 Answers1

0

what is the version of your selenium,Assuming latest version find_elements_by_link_text has been abolish from selenium 4.3.0

webbrowser module is part of python standard library. if you use it, you will not be able to use selenium, make use of webdriver instead these two line of code should change from

links = driver.find_elements_by_link_text('Download File')
webbrowser.open(URL)

to

links = driver.find_elements(By.LINK_TEXT, "Download File")
driver.get(URL)

and makes sure to import

from selenium.webdriver.common.by import By

the full code should be this

import webbrowser
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
##location of google chrome example
options.binary_location = r"C:\\Users\\GoogleChrome.exe"
##chrome driver location example
service = Service(executable_path=r'C:\\Users\\python\\chromedriver\\ChromeDriver 106.0.5249.61\\chromedriver.exe')
caps = options.to_capabilities()
driver = webdriver.Chrome(service=service, options=options,desired_capabilities=caps)
# Not an actual link for public purposes
URL = "https://banfaouf.com/lfmamdofpisao"
driver.get(URL)

# Find all links that contain specific text (e.g., 'Click Me' in this example)
links = driver.find_elements(By.LINK_TEXT, 'Download File')


# Add a time delay (in seconds) between clicks
time_delay_between_clicks = 2  # Adjust this value as needed (e.g., 2 seconds)

# Click each link with a time delay between clicks
for link in links:
    link.click()
    time.sleep(time_delay_between_clicks)
RF1991
  • 2,037
  • 4
  • 8
  • 17