简体   繁体   English

如何使用 selenium、python 列出网站中的所有可点击链接?

[英]How to list all clickable links in a website using selenium, python?

I am new to selenium.我是 selenium 的新手。 I want to navigate to www.cisco.com and list all the clickable buttons by their names (and respective xpaths too if possible) in a list like我想导航到www.cisco.com并在类似

['Log in','Products and services','Solutions',....etc. ['登录','产品和服务','解决方案',....等。 ] ]

I can navigate to the site using a chrome browser exe as given below:我可以使用 chrome 浏览器 exe 导航到该站点,如下所示:

from selenium import webdriver
from selenium.webdriver.common.by import By
chromedriver_location = "C:\\Path\\To\\chromedriver"

driver = webdriver.Chrome(chromedriver_location)
driver.get('https://cisco.com/')

first_popup_close='//*[@id="onetrust-close-btn-container"]/button'
supposedly_front_page='//*[@id="wcq"]'
#Close the pop-up
driver.find_element("xpath", first_popup_close).click()

#If I am not wrong in selecting the XPATH..
ids = driver.find_elements(By.XPATH, supposedly_front_page)

for i in ids:
    #print i.tag_name
    print(i.get_attribute("name"))    # Does not work as expected

I even tried using find_element(By.LINK_TEXT, "Log in") , find_element(By.PARTIAL_LINK_TEXT, "Log") etc. just to find only the 'Log in' button (in case my front page XPATH may be wrong), but it does not return the output (empty list).我什至尝试使用find_element(By.LINK_TEXT, "Log in")find_element(By.PARTIAL_LINK_TEXT, "Log")等只找到“登录”按钮(以防我的首页 XPATH 可能是错误的),但它不返回 output(空列表)。
The issue is - selenium doc has vague information about such or may be I am not able to find the required info.问题是 - selenium 文档有关于此类的模糊信息,或者我可能无法找到所需的信息。 The older posts about the same does not work mostly as lots of functions have been deprecated by now.相同的旧帖子大多不起作用,因为许多功能现在已被弃用。

If you like to get the login button for example from this page do:例如,如果您想从此页面获取登录按钮,请执行以下操作:

loginbtn = driver.find_element_by_xpath("//button[@id='fwt-profile-button']")

or if you'd like to get all buttons you can identify them by doing:或者,如果您想获取所有按钮,您可以通过以下方式识别它们:

btns = driver.find_elements_by_xpath("//button")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM