1. 程式人生 > >python+selenium之定位web頁面元素三:By 定位元素

python+selenium之定位web頁面元素三:By 定位元素

用 By 定位元素

有時需要使用定位方法,在具體通過哪種定位方式(id 或 name)根據實際場景而定位,By 就可以設

置定位策略。

find_element(By.ID,"kw")
 find_element(By.NAME,"wd")
 find_element(By.CLASS_NAME,"s_ipt") 
find_element(By.TAG_NAME,"input") 
find_element(By.LINK_TEXT,u"新聞") 
find_element(By.PARTIAL_LINK_TEXT,u"新") 
find_element(By.XPATH,"//*[@class='bg s_btn']") 
find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")

find_element()方法只用於定位元素。它需要兩個引數,第一個引數是定位方式,這個由 By 提供;另

第二個引數是定位的值。在使用 By 時需要將 By 類匯入。

from selenium.webdriver.common.by import By