1.获取页面title
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
print(browser.title)
2.获取页面URL
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https:
#打印网页标题
print(browser.current_url)
#输出内容:https:
3.获取浏览器版本号
- capabilities['version']):打印浏览器version的值
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
print(browser.capabilities['version'])
4.获取元素尺寸
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
input_box = browser.find_element_by_id('kw')
print(input_box.size)
5.获取元素的文本
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
recordcode = browser.find_element_by_id('jgwab')
print(recordcode.text)
6.获得属性值
- get_attribute('')方法
- get_attribute('href'):获取href属性值
- get_attribute('id'):获取id属性值
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()