Selenium:如何从列表中单击单个项目'find_elements'



我遇到了一个问题,我有一个元素列表:

查找元素:

arialabel = (list([my_elem.get_attribute("aria-label") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.MatchModeQuestionGridTile-content>div[aria-label]")))]))

输出

['solid', 'massive; solid', 'hazy, indistinct', 'akimbo', 'monolithic', 'hands on hips, elbows bent', 'inimical', 'Nebulous', 'to explode', 'hostile; harmful', 'state of matter', 'fulminate']

我知道列表中每个相应项目的位置,因为这是一个混合术语和定义的列表,所以当我尝试点击一个元素时,使用这个:

for _ in sort_order:
pos1sort = sort_order[tempVar2]
sortedList = [i for i in range(len(sort_order)) if sort_order[i] == pos1sort]
arialabel.click([sortedList[0]])
arialabel.click([sortedList[1]])
print(sortedList)
tempVar2+=1

我当然会出错说AttributeError: 'list' object has no attribute 'click'

这些是列表中项目的相应位置。

[1, 4]
[2, 9]
[3, 7]
[1, 4]
[0, 5]
[6, 8]
[3, 7]
[6, 8]
[2, 9]
[10, 11]
[10, 11]

很抱歉问了这么长的问题,但我想知道如何使用selenium中的click((函数来点击列表中的特定项目,就像我尝试的那样。谢谢!

找到了

容器只是分配给查找元素函数的另一个变量

container = driver.find_elements_by_class_name('MatchModeQuestionGridTile-content')
for _ in sort_order:
sorted1 = int(sortedList[0])
sorted2 = int(sortedList[1])
container[sorted1].click()
container[sorted2].click()

最新更新