selenium元素定位篇 class定位

2022-06-07 11:03:06 字數 817 閱讀 6549

在前端的單個頁面中,通過id定位是萬無一失的,因為id值是唯一的。但是,並不是每個元素都有id屬性,所以我們可以通過其他元素進行元素定位。

今天要說的就是元素定位方法之一:class定位。

在python中,通過class定位的方法如下:

def

find_element_by_class_name(self, name):

"""finds an element by class name.

:args:

- name: the class name of the element to find.

:returns:

- webelement - the element if it was found

:raises:

- nosuchelementexception - if the element wasn't found

:usage:

element = driver.find_element_by_class_name('foo')

"""return self.find_element(by=by.class_name, value=name)

引數name:元素的class屬性值。

返回值:如果找到元素,則返回webelement物件。否則,丟擲nosuchelementexception異常。

用法舉例:element = driver.find_element_by_class_name("foo")

拓展:selenium中,八種定位方式最終都是通過呼叫find_element(by, value)方法

selenium元素定位篇 name定位

前面說過,除了通過id定位之外,還可以通過其他元素的屬性進行定位,前面說了class定位,這次說一下name屬性定位。在python中,通過name屬性定位的方法如下 def find element by name self,name finds an element by name.args n...

selenium元素定位篇 id定位

在前端,id是唯一的,只屬於乙個元素。在python中,元素定位的方法如下 def find element by id self,id finds an element by id.args id the id of the element to be found.returns webeleme...

Selenium元素定位 提高篇 CSS定位

css cascading style sheets 是一種用於渲染 html 或者 xml 文件的語言,css 利用其選擇器可以將樣式屬性繫結到文件中的指定元素。理論上說無論乙個元素定位有多複雜都能夠定位到元素。因為不同的瀏覽器 xpath 引擎不同甚至沒有自己的 xpath 引擎,這就導致了 x...