#coding = utf-8 from selenium import webdriver import time class driver_1(object): #生成一个对象 def __init__(self): self.driver = webdriver.Chrome("D:chromedriver_win32chromedriver.exe") def driver_size(self): self.driver.get('https://www.baidu.com') #返回元素的尺寸(size) size = self.driver.find_element_by_css_selector('#kw[name="wd"]').size time.sleep(3) self.driver.close() return size def driver_text(self): self.driver.get('https://www.baidu.com') text_list = [] #返回元素文本 text = self.driver.find_elements_by_css_selector('div.s-bottom-layer-left p > a') for i in text: text_list.append(i.text) time.sleep(3) self.driver.close() return text_list def driver_attribute(self): self.driver.get('https://www.cnblogs.com/') attribute_list = [] #获取属性值,可以是id,name,type或元素拥有的其他任意属性 attribute = self.driver.find_elements_by_css_selector('#nav_menu a') for i in attribute: attribute_list.append(i.get_attribute('href')) time.sleep(3) self.driver.close() return attribute_list if __name__ == "__main__": a = driver_1() resault = a.driver_attribute() print(resault)