# Python 爬虫使用 Selenium 如何在 WebElement 获得属性

**URL:** <https://www.isharkfly.com/t/python-selenium-webelement/14980>\
**Category:** Python\
**Tags:** python\
**Created:** [September 19, 2023, 4:41am UTC](https://www.isharkfly.com/t/python-selenium-webelement/14980 "2023-09-19T04:41:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [September 19, 2023, 4:41am UTC](https://www.isharkfly.com/t/python-selenium-webelement/14980/1 "2023-09-19T04:41:48Z")

</div>

首先，我们需要初始化驱动和指定使用特定的流量器。

 ![](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/1/1b856a11ed791a6a4776ef338a028e780549b838.jpeg)  
代码如下：

```auto
from selenium import webdriver
wd = webdriver.Firefox()

```

上面的代码可以简单的理解为启动一个 Firefox 的实例。

## 使用 css 选择器

可以把程序读取的 HTML 理解为一个 Doc。

我们需要在 Doc 中选择我们的元素，这个叫做选择器，通常来说 HTML 很多不同的选择器。

```auto
elem = wd.find_element_by_css_selector('#my-id')

```

上面的代码是使用 css 的选择器。

## 获得属性

但我们使用选择器获得元素后，下一步就是我们需要获得属性了。

Python 的代码为：

```auto
element.get_attribute('innerHTML')

```

这样我们就可以通过元素获得属性了。
