Skip to main content

Selenium入门6 操作元素,获取元素属性

取元素的text,属性get_attribute,标签名tag_name

操作元素:send_keys输入,click点击,submit提交,clear清除输入

状态判断:is_display,is_enabled,is_selected

#coding=utf-8
#元素的操作

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://cn.bing.com")

#打印logo的文字
print("text of logo is",driver.find_element_by_class_name("hp_sw_logo").text)
#搜索框send_keys,clear,tabname,id,title等属性和方法
search_box = driver.find_element_by_class_name("b_searchbox")
search_box.send_keys("selenium入门")
time.sleep(1)#清除输入
search_box.clear()
time.sleep(1)
search_box.send_keys("测试入门")
time.sleep(1)#各种属性和状态
print("tag is",search_box.tag_name)
print("id is",search_box.id)
print("title is",search_box.get_attribute("title"))
print("searchbox isenalbed",search_box.is_enabled())
print("searchbox displayed",search_box.is_displayed())
print("searchbox selected",search_box.is_selected())
#按钮click,submit
botton = driver.find_element_by_id("sb_form_go")
#botton.click()
botton.submit()