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

标签 python python-3.x selenium selenium-webdriver

使用这些命令,我​​多次打印了“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'

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

关于python - 如何一次打印()一个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54900958/

相关文章:

python - 在 python 中求解一个简单的符号方程

Python 百分比舍入

python - 为 python Web 应用程序运行 apache 服务器时导入错误

python - 如何使用 pyparsing 来解析具有多种开启/关闭类型的嵌套表达式?

python - 2D 数组中的 Matplotlib 4D 数据

python - 无法使用正则表达式获得自定义结果

python - 如果不在括号之间则匹配字符

android - 在 Appium/Selendroid 中使用 "copy"和 "paste"

java - 报告从 Jenkins 管道作业运行的多个 Selenium Webdriver 作业

Python selenium 不会切换帧