【学习笔记】APP页面,随机选择一个选项

如下页面,一个测评功能,30个题目,每题的答案选项一样:

要实现每题自动随机选择一个答案

方法一:定义一个5个选项的列表,循环30次,然后使用random.shuffle打乱列表顺序,根据列表元素定位第几个选项

a = ['完全同意', '同意', '不知道', '不同意', '完全不同意']
for i in range(0, 30):
random.shuffle(a)
if a[0] == "完全同意":
driver.find_element_by_xpath('//*[@text="完全同意"]').click()
if a[0] == "同意":
driver.find_element_by_xpath('//*[@text="同意"]').click()
if a[0] == "不知道":
driver.find_element_by_xpath('//*[@text="不知道"]').click()
if a[0] == "不同意":
driver.find_element_by_xpath('//*[@text="不同意"]').click()
if a[0] == "完全不同意":
driver.find_element_by_xpath('//*[@text="完全不同意"]').click()
方法二:定位到所有得元素,组成一个列表,用random.choice随机取
a = '//*[@text="完全同意"]'
b = '//*[@text="同意"]'
c = '//*[@text="不知道"]'
d = '//*[@text="不同意"]'
e = '//*[@text="完全不同意"]'
list = [a, b, c, d, e]
for i in range(0, 30):
ell = random.choice(list)
driver.find_element_by_xpath(ell).click()
方法三:成一个1-5的随机数,然后根据随机数定位第几个选项(和方法一类似)
for i in range(0, 30):
a = random.randint(1,5)
    if a == 1:
driver.find_element_by_xpath('//*[@text="完全同意"]').click()
if a == 2:
driver.find_element_by_xpath('//*[@text="同意"]').click()
if a == 3:
driver.find_element_by_xpath('//*[@text="不知道"]').click()
if a == 4:
driver.find_element_by_xpath('//*[@text="不同意"]').click()
if a == 5:
driver.find_element_by_xpath('//*[@text="完全不同意"]').click()


posted @ 2019-10-10 14:25  libre_yop  阅读(431)  评论(0编辑  收藏  举报