1. 程式人生 > >Python+Selenium學習筆記4

Python+Selenium學習筆記4

AS keys print () baidu sel 可用 value bdr

1.submit()

submit()方法用於提交表單。如在搜索框輸入關鍵字後按回車鍵進行查詢操作,就可用submit()方法模擬。若不能按回車鍵進入下一步,則不能用submit()舉例,百度的查詢提交。

1 from selenium import webdriver
2 
3 dr = webdriver.Firefox()
4 dr.get("http://www.baidu.com")
5 
6 dr.find_element_by_id("kw").send_keys("selenium")
7 dr.find_element_by_id("kw").submit()
8 dr.close()

submit()可與click()互換,但click()的應用範圍更廣泛

2.get_attribute()

獲取元素的屬性值

 1 from selenium import webdriver
 2 
 3 dr = webdriver.Firefox()
 4 dr.get("http://www.baidu.com")
 5 
 6 attr1 = dr.find_element_by_id("kw").get_attribute("id")
 7 print("attr1 = %s" % attr1)
 8 attr2 = dr.find_element_by_id("kw").get_attribute("
name") 9 print("attr2 = %s" % attr2) 10 attr3 = dr.find_element_by_id("kw").get_attribute("class") 11 print("attr3 = %s" % attr3) 12 attr4 = dr.find_element_by_id("kw").get_attribute("value") 13 print("attr4 = %s" % attr4) 14 dr.close()

Python+Selenium學習筆記4