selenium查找元素的基本操作


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/30 21:15
# @Author : Lhtester
# @Site : 
# @File : try.py
# @Software: PyCharm
from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()
browser.get('http://www.taobao.com')

#查找单个元素
input_first = browser.find_element_by_id('q')
input_second = browser.find_element_by_css_selector('#q')
input_third = browser.find_element(By.ID,'q')

print(input_first)
print(input_second)
print(input_third)

#查找多个元素
lis= browser.find_elements_by_css_selector('li')
lis_c = browser.find_element(By.CSS_SELECTOR,'li')

print(lis)
print(lis_c)

a = browser.find_element_by_xpath('//div[@class="search-hots-fline"]/a[1]')
#获取属性
print(a.get_attribute('href'))
#获取文本
print(a.text)
#获取位置
print(a.location)
#获取id
print(a.id)
#获取size
print(a.size)
#获取标签名
print(a.tag_name)

import time
time.sleep(20)
browser.quit()