selenium模块,web自动化,元素定位

1. 元素定位

查看网页元素

右键-->检查

from selenium.webdriver.common.by import By  # 元素定位包

# 使用
test.find_element(By.XXX)

 

1)定位元素ID--对应浏览器id

# 定位一个元素
a = test.find_element(By.ID, value="wrapper")
print(a)
# 定位多个元素(返回列表)
b = test.find_elements(By.ID, value="wrapper")
print(b)

① 例子

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options # 用于设置谷歌浏览器
from selenium.webdriver.common.by import By  # 元素定位包

import time
def chrom():
    # 创建设置浏览器对象
    chrome_set = Options()
    # 禁用沙河模式(提高兼容性)
    chrome_set.add_argument("--no-sandbox")
    # 保持浏览器打开状态(默认代码执行完毕后自动关闭)
    chrome_set.add_experimental_option(name="detach", value=True)
    # 创建并启动浏览器
    chrome = webdriver.Chrome(executable_path="chromedriver.exe", options=chrome_set)
    return chrome

test = chrom()
# 打开指定网站
test.get("https://www.baidu.com")
time.sleep(1)
# 关闭当前标签页

# test.get_screenshot_as_file("1.png")
# test.refresh()
# test.close()

# 定位一个元素
a = test.find_element(By.ID, value="wrapper")
print(a)
# 定位多个元素(返回列表)-- 需要进行切片处理
b = test.find_elements(By.ID, value="wrapper") 
print(b)
# 退出浏览器(关闭所有标签页)
time.sleep(
2)
test.quit()

② 浏览器定位多个元素(浏览器控制台查看)

# 浏览器定位多个元素(浏览器控制台查看)
document.getElementById("wrapper")

 2. 元素输入


# 定位一个元素
a = test.find_element(By.ID, value="kw")
print(a)
# 元素输入 a.send_keys(
"豆瓣") # 定位到按钮 b = test.find_element(By.ID, value="su") # 元素点击 b.click() # 元素清空 a.clear()
from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options # 用于设置谷歌浏览器
from selenium.webdriver.common.by import By  # 元素定位包

import time
def chrom():
    # 创建设置浏览器对象
    chrome_set = Options()
    # 禁用沙河模式(提高兼容性)
    chrome_set.add_argument("--no-sandbox")
    # 保持浏览器打开状态(默认代码执行完毕后自动关闭)
    chrome_set.add_experimental_option(name="detach", value=True)
    # 创建并启动浏览器
    chrome = webdriver.Chrome(executable_path="chromedriver.exe", options=chrome_set)
    return chrome

test = chrom()
# 打开指定网站
test.get("https://www.baidu.com")
time.sleep(1)


# 定位一个元素
a = test.find_element(By.ID, value="kw")
print(a)
a.send_keys("豆瓣")
# 定位到按钮
b = test.find_element(By.ID, value="su")
# 元素点击
b.click()

# 元素清空
a.clear()
a.send_keys("电影")

# 退出浏览器(关闭所有标签页)
time.sleep(2)
test.quit()

 

 

 

3.元素定位NAME--对应浏览器name

# 定位一个元素
a = test.find_element(By.NAME, value="wd")
print(a)

 

4. 元素定位CLASS_NAME--对应浏览器class

# 注意:class内容不能有空格
a = test.find_element(By.CLASS_NAME, value="s_ipt") print(a)

 

5.元素定位TAG_NAME--对应浏览器html中的标签

# 定位一个元素
a = test.find_element(By.TAG_NAME, value="input")
print(a)
# 定位多个元素,需要切片处理
a = test.find_elements(By.TAG_NAME, value="input")[0]
print(a)

 

6.元素定位LINK_TEXT(精准定位)--对应浏览器的文字链接

a = test.find_element(By.LINK_TEXT, value="新闻")
a.click()

 

7.元素定位PARTIAL_LINK_TEXT(模糊定位)--对应浏览器的文字链接

a = test.find_element(By.PARTIAL_LINK_TEXT, value="")
a.click()

 

8.元素定位CSS_SELECTOR

① #id = 通过id定位

a = test.find_element(By.CSS_SELECTOR, value="#kw")

 

②.class = 通过class定位

a = test.find_element(By.CSS_SELECTOR, value=".s_ipt")

③ 不加修饰 = 通过标签头定位

a = test.find_element(By.CSS_SELECTOR, value="input")

④ 通过任意类型定位

# 格式 "[类型='精准值']"
a = test.find_element(By.CSS_SELECTOR, value="[maxlength='255']")

# 格式 "[类型*='模糊值']"
a = test.find_element(By.CSS_SELECTOR, value="[maxlength*='255']")

# 格式 "[类型^='开头值']"
a = test.find_element(By.CSS_SELECTOR, value="[maxlength^='255']")

# 格式 "[类型$='结尾值']"
a = test.find_element(By.CSS_SELECTOR, value="[maxlength$='255']")

# 更简单定位方式
浏览器-->检查-->选择要定位的位置-->右键-->copy-->selector(就用这个值)
a = test.find_element(By.CSS_SELECTOR, value="复制的值")

 

 

9.元素定位XPATH(属性+路径定位--属性如果是随机的,就会出错)

# 属性+路径定位--属性如果是随机的,就会出错
浏览器-->检查-->选择要定位的位置-->右键-->copy-->xpath(就用这个值) a = test.find_element(By.XPATH, value="复制的值")


# 纯路径定位(一般不会出错)
浏览器-->检查-->选择要定位的位置-->右键-->copy-->full xpath(就用这个值)
a = test.find_element(By.XPATH, value="复制的值")

 

10. 元素定位的隐形等待

# 有些网站访问后元素不会立即出现,需要设置一下等待出现的时间,30s内能找到就操作,不一定是等待30s,比time.sleep(30)更方便,快
test.implicitly_wait(30)

 

1)例

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options # 用于设置谷歌浏览器
from selenium.webdriver.common.by import By  # 元素定位包
import time
def chrom():
    # 创建设置浏览器对象
    chrome_set = Options()
    # 禁用沙河模式(提高兼容性)
    chrome_set.add_argument("--no-sandbox")
    # 保持浏览器打开状态(默认代码执行完毕后自动关闭)
    chrome_set.add_experimental_option(name="detach", value=True)
    # 创建并启动浏览器
    chrome = webdriver.Chrome(executable_path="chromedriver.exe", options=chrome_set)
    return chrome

test = chrom()
# 打开指定网站
test.get("https://www.baidu.com")
# time.sleep(1)

# 定位一个元素
test.implicitly_wait(30)
a = test.find_element(By.CSS_SELECTOR, value='#kw')
a.send_keys("电影")



time.sleep(2)
test.quit()

 

posted @ 2024-11-23 16:44  铿锵有力自信且坚定  阅读(78)  评论(0)    收藏  举报