Selenium 自动化测试:识别并提取页面下拉框和输入框信息
selects_attributes = {}
selects_attributes_ele = {}
selects = driver.find_elements(By.TAG_NAME, 'dt')
print('下拉框数量:', len(selects))
for element in selects:
if not element.is_displayed(): # text == ''
continue
text = element.text
location = element.location
describe = Input_describe(location, text_attributes)
if text == '':
text = describe.split('。')[0]
if text in selects_attributes:
# if location not in [t['coordinate'] for t in selects_attributes[text]]:
# if abs(location['x'] - selects_attributes[text][-1]['coordinate']['x']) < 15 or abs(
# location['y'] - selects_attributes[text][-1]['coordinate']['y']) < 15:
# continue
selects_attributes[text].append(
{'coordinate': (location['x'], location['y']), 'describe': Input_describe(location, text_attributes)})
# selects_attributes[text].append(
# {'coordinate': (location['x'], location['y']), 'id': element.id, 'class': element.get_attribute('class'),
# 'describe': Input_describe(location, text_attributes)})
selects_attributes_ele[text].append(
{'coordinate': (location['x'], location['y']), 'id': element.id,
'class': element.get_attribute('class'),
'describe': Input_describe(location, text_attributes), 'element': element})
else:
selects_attributes[text] = [
{'coordinate': (location['x'], location['y']), 'describe': Input_describe(location, text_attributes)}]
# driver.execute_script('arguments[0].click();', element)
# selects_attributes[text] = [
# {'coordinate': (location['x'], location['y']), 'id': element.id, 'class': element.get_attribute('class'),
# 'describe': Input_describe(location, text_attributes)}]
selects_attributes_ele[text] = [
{'coordinate': (location['x'], location['y']), 'id': element.id,
'class': element.get_attribute('class'),
'describe': Input_describe(location, text_attributes), 'element': element}]
print(selects_attributes)
# inputs = driver.find_elements(By.TAG_NAME, 'input')
inputs = driver.find_elements(By.XPATH, '//input | //textarea')
print('输入框数量:', len(inputs))
inputs_attributes = {}
inputs_attributes_ele = {}
for element in inputs:
if element.is_displayed() and element.get_attribute('type') != 'hidden':
key = element.get_attribute('value')
location = element.location
describe = Input_describe(location, text_attributes)
if key == '':
key = describe.split('。')[0]
skip = False
for select_text, select_attributes in selects_attributes.items():
for select_attribute in select_attributes:
select_coordinate = select_attribute['coordinate']
distance = ((location['x'] - select_coordinate[0]) ** 2 + (location['y'] - select_coordinate[1]) ** 2) ** 0.5
if distance < 10:
skip = True
break
if skip:
break
if skip:
continue
if key in inputs_attributes:
if abs(location['x'] - inputs_attributes[key][-1]['coordinate']['x']) < 15 and abs(
location['y'] - inputs_attributes[key][-1]['coordinate']['y']) < 15:
continue
inputs_attributes[key].append({'coordinate': (location['x'], location['y']),
'data-placeholder': element.get_attribute('data-placeholder'),
'type': element.get_attribute('type'),
'accessible_name': element.accessible_name,
'describe': describe + '。' + element.accessible_name
})
inputs_attributes_ele[key].append({'id': element.get_attribute('id'),
'coordinate': (location['x'], location['y']),
'data-placeholder': element.get_attribute(
'data-placeholder'),
'type': element.get_attribute('type'),
'accessible_name': element.accessible_name,
'class': element.get_attribute('class'),
'describe': describe + '。' + element.accessible_name,
'element': element
})
else:
inputs_attributes[key] = [{'coordinate': (location['x'], location['y']),
'data-placeholder': element.get_attribute('data-placeholder'),
'type': element.get_attribute('type'),
'accessible_name': element.accessible_name,
'describe': describe + '。' + element.accessible_name
}]
inputs_attributes_ele[key] = [{'id': element.get_attribute('id'),
'coordinate': (location['x'], location['y']),
'data-placeholder': element.get_attribute('data-placeholder'),
'type': element.get_attribute('type'),
'accessible_name': element.accessible_name,
'class': element.get_attribute('class'),
'describe': describe + '。' + element.accessible_name,
'element': element
}]
print(inputs_attributes)
代码中,首先通过 driver.find_elements(By.TAG_NAME, 'dt') 获取页面中的所有 dt 标签元素,这些元素通常代表下拉框。然后遍历每个 dt 元素,提取其文本内容、坐标位置、描述文本等信息,并存储到 selects_attributes 和 selects_attributes_ele 字典中。
接着,通过 driver.find_elements(By.XPATH, '//input | //textarea') 获取页面中的所有输入框元素,遍历每个输入框元素,提取其属性信息,例如 value、data-placeholder、type、accessible_name 等。在遍历输入框元素时,代码中还添加了判断条件,如果输入框元素的坐标位置与 selects_attributes 字典中存储的下拉框元素坐标位置的欧式距离小于 10,则跳过该输入框元素,避免重复提取。
最后,代码将提取到的下拉框和输入框信息分别存储到 selects_attributes 和 inputs_attributes 字典中,方便后续使用。
该代码可以帮助您快速识别网页中的下拉框和输入框元素,并提取其属性信息,从而方便您进行自动化测试或其他网页分析工作。
原文地址: http://www.cveoy.top/t/topic/pkzK 著作权归作者所有。请勿转载和采集!