Selenium gets the href attribute of page elements

This article introduces how to obtain a certain attribute of a page element through Selenium. An element may have multiple attributes, such as class, id, name, text, href, vale, etc. Here we give an example of a link problem that often needs to be dealt with in crawlers: find out all the hyperlinks on the current page.

Take the Baidu homepage as an example, print links to all elements that contain href.

The relevant script code is as follows:

# coding=utf-8
import time
from selenium import webdriver
 
 
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com")
time.sleep(1)
 
for link in driver.find_elements_by_xpath("//*[@href]"):
    print (link.get_attribute('href'))
driver.quit()

Here is just to find the href attribute of the element. If you need other attributes, for example, if you need to view all the elements on the page with id values, you can write like this

print (link.get_attribute('id'))

Guess you like

Origin blog.csdn.net/zhuan_long/article/details/110129137