AttributeError:类型对象“WebDriver”没有属性“find_elements_by_link_text

问题描述 投票:0回答:1

通过链接文本编写查找元素的更新方法是什么? 长话短说...我在网站的各个页面上有大约 4000 个链接可供点击。 (如果我可以自动执行此操作,我可以将其用作其余常见链接文本以及其他页面上的模板)

具体来说,我需要知道我哪里出了问题?从事这个已经有一段时间了。我决定认输,但我找不到像我的 mac 终端/视觉工作室/pylance 这样的解决方法。他们都喜欢大喊大叫,哈哈。无论如何,我使用的是 MacBook Pro 笔记本电脑,我非常感谢您的帮助!

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 paltform!) Idk if this makes any difference but among factors to consider:

MacBook Pro (all up to date) along with Latest Version of Python, Selenium,VSC, chrome driver all installed & Mac Terminal is also in working order
python selenium-webdriver hyperlink selenium-chromedriver attributeerror
1个回答
0
投票

你的selenium版本是什么 假设最新版本这部分代码应该改变,它 find_elements_by_link_text 已从

selenium 4.3.0

废除
links = driver.find_elements_by_link_text('Download File')

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

并确保导入

from selenium.webdriver.common.by import By

selenium 适用于 webdriver 而不是 webbrowser 。 完整的代码应该是这个

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\\GoogleChromePortable.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)
© www.soinside.com 2019 - 2024. All rights reserved.