使用Selenium获取页面中的文本、输入框、下拉框及其对应的坐标,可以通过以下步骤编写代码:

  1. 导入必要的库和模块
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
  1. 初始化WebDriver,打开浏览器
driver = webdriver.Chrome()  # 初始化Chrome浏览器
driver.get("https://example.com")  # 打开目标网页
  1. 获取页面中的所有文本和元素
# 获取页面中的文本
all_text = driver.find_element_by_tag_name('body').text

# 获取页面中的所有输入框和下拉框
inputs = driver.find_elements_by_tag_name('input')
selects = driver.find_elements_by_tag_name('select')
  1. 获取文本、输入框和下拉框的坐标
# 获取文本的坐标
text_location = driver.find_element_by_tag_name('body').location

# 获取输入框和下拉框的坐标
input_locations = [input.location for input in inputs]
select_locations = [select.location for select in selects]

完整的代码如下:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("https://example.com")

# 获取页面中的文本
all_text = driver.find_element_by_tag_name('body').text

# 获取页面中的所有输入框和下拉框
inputs = driver.find_elements_by_tag_name('input')
selects = driver.find_elements_by_tag_name('select')

# 获取文本的坐标
text_location = driver.find_element_by_tag_name('body').location

# 获取输入框和下拉框的坐标
input_locations = [input.location for input in inputs]
select_locations = [select.location for select in selects]

print("页面中的文本:", all_text)
print("输入框的坐标:", input_locations)
print("下拉框的坐标:", select_locations)

driver.quit()

请注意,这段代码仅适用于获取页面中的文本、输入框和下拉框的坐标,对于其他类型的元素可能需要进行相应的修改。另外,为了获取准确的坐标,可能需要在代码中加入等待时间或使用显式等待


原文地址: https://www.cveoy.top/t/topic/irVd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录