Skip to main content
 首页 » 编程设计

python之如何一次打印()一个命令

2025年05月04日10落叶无声

使用这些命令,我​​多次打印了“Hello”这个词

one=driver.find_elements_by_xpath("any") 
for two in one: 
    if two.text=='three' 
        print('Hello')  
    else: 
        print('Bye') 

我如何更改代码,以便如果所有元素 == "three"都打印一次 "Hi",并且如果有一个元素 that! = "Three", 打印一次 "Bye"

请您参考如下方法:

您可以将一个设为一组并比较结果

one = driver.find_elements_by_xpath("any") 
two = {_.text for _ in one} 
if len(two) == 1 and two[0] == 'three': 
    print('hi') 
else: 
    print('bye') 

实际上,这需要 ('three', 'three', 'three') 并将其转换为 {'three'} 或采用 ('three ', 'three', 'not three') 并将其转换为 {'three', 'not three'}。然后你看看它是否只有一个元素长,那个元素是不是 'three'。

希望如果需要这个解释,它会有所帮助。