# 5. 单选问题处理
# 假定来到了单选问题页面
def select_single_choice_question(self,driver, question_text):
"""
根据问题文本选择单选问题的一个随机选项。
如果选项超过一屏,尝试向上滑动页面。
:param driver:
:param question_text: 问题的文本
"""
# 定位问题
question_element = driver.find_element(MobileBy.XPATH,
f'//android.widget.TextView[contains(@text, "{question_text}")])[2]')
if not question_element:
print(f"问题 '{question_text}' 未找到。")
return
# 定位问题下方的所有单选按钮
options_xpath= f'//android.widget.TextView[contains(@text, "{question_text}")])[2]/following-sibling::android.view.View/android.view.View'
# options_xpath = f'{question_element}.//following-sibling::*[@class="android.widget.RadioButton"]'
try:
options = question_element.find_elements(MobileBy.XPATH, options_xpath)
except NoSuchElementException:
print(f"未找到与问题 '{question_text}' 相关的选项。")
return
# 如果选项数量超过预设值(例如13个,这里仅作示例),尝试滑动页面
if len(options) > 13:
# 计算滑动起始和结束点(根据实际屏幕尺寸调整)
screen_width, screen_height = driver.get_window_size().values()
start_x = screen_width / 2
start_y = screen_height * 0.8 # 起始位置接近底部
end_y = screen_height * 0.3 # 结束位置接近顶部
driver.swipe(start_x=start_x, start_y=start_y, end_x=start_x, end_y=end_y, duration=1000)
# 随机选择一个选项点击
selected_option = random.choice(options)
selected_option.click()