selenium获取标签class
时间: 2023-09-12 17:13:32 浏览: 248
使用selenium获取标签的class属性,可以使用find_element(s)_by_class_name方法。其中,find_element_by_class_name方法返回匹配的第一个元素,find_elements_by_class_name方法返回所有匹配的元素的列表。示例代码如下:
```python
from selenium import webdriver
# 创建浏览器驱动对象
driver = webdriver.Chrome()
# 打开网页
driver.get("https://example.com")
# 获取class为"example-class"的第一个元素
element = driver.find_element_by_class_name("example-class")
# 获取所有class为"example-class"的元素列表
elements = driver.find_elements_by_class_name("example-class")
```
以上代码中,"example-class"为要匹配的class名称,可以根据实际情况进行修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [selenium提取数据的方法总结](https://blog.csdn.net/CC_Cynthia/article/details/104343161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [selenium提取数据之driver对象定位标签元素获取标签对象的方法](https://blog.csdn.net/weixin_44799217/article/details/112386419)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文