是否可以单击硒中一个接一个的多个按钮列表?

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

我是一名学生,正在使用python上的硒进行抓取项目。我正在尝试从多个格式相同的多个配置文件中抓取数据。有一个目录网站,带有指向所有配置文件的按钮,但是我遇到的问题是一次可以单击每个按钮,因为它们的格式都相同。我的目标是能够在新选项卡中打开第一个链接,从该配置文件中抓取数据,然后关闭第一个配置文件的选项卡,然后再移至第二个。我希望这个过程可以重复。这是我到目前为止的内容:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path="MY PATH TO MY CHROME DRIVER")
driver.implicitly_wait(5)
driver.get("http://directory.bcsp.org")

buttons = driver.find_elements_by_link_text('View Profile')

请让我知道您是否对我的问题有任何解决方案。谢谢:)

python selenium selenium-webdriver web-scraping repeat
1个回答
0
投票

“正文”以显示在页面上

“ profile_count”获取链接

“。get_attribute('href')”用于从“查看个人资料”按钮中抓取链接]

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

driver = webdriver.Edge()
driver.get("https://directory.bcsp.org/")
count = int(input("Count : "))

body = driver.find_element_by_xpath("//body") #
profile_count = driver.find_elements_by_xpath("//div[@align='right']/a")

while len(profile_count) < count: # Get links up to "count"
    body.send_keys(Keys.END)
    sleep(1)
    profile_count = driver.find_elements_by_xpath("//div[@align='right']/a")
    driver.close()
for link in profile_count: # Calling up links
    driver = webdriver.Edge()
    driver.get(link.get_attribute('href'))
    #   you can do
    #   what you want
    #   to do here
    driver.close()
© www.soinside.com 2019 - 2024. All rights reserved.