前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Python+Selenium页面元素查找

Python+Selenium页面元素查找

原创
作者头像
coffee1
修改2024-12-27 09:29:18
修改2024-12-27 09:29:18
1100
举报

网上的过期用法

username = driver.find_element_by_name('username')

login_form = driver.find_element_by_id('loginForm')

最新的有效用法

wd.find_element(By.XPATH, '//button[text()="Some text"]')

wd.find_element(By.ID, 'button')

wd.find_element(By.NAME, 'button')

有效用法示例

from selenium import webdriver

from selenium.webdriver.common.by import By

wd = webdriver.Chrome()

# 调用webpriver 对象的get方法 可以让浏览器打开指定网址

wd.get('https://www.baidu.com')

time.sleep(60)

wd.find_element(By.XPATH, '//button[text()="Some text"]')

wd.find_element(By.ID, 'button')

wd.find_element(By.NAME, 'button')

下面是 By 类的一些可用属性:

ID = "id"

XPATH = "xpath"

LINK_TEXT = "link text"

PARTIAL_LINK_TEXT = "partial link text"

NAME = "name"

TAG_NAME = "tag name"

CLASS_NAME = "class name"

CSS_SELECTOR = "css selector"

find_element使用By类的使用方式

wd.find_element(LINK_TEXT, 'text1')

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档