Python 如何打印()一次命令
使用这些命令,我可以多次打印Hello这个词Python 如何打印()一次命令,python,python-3.x,selenium,selenium-webdriver,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') 如何更改代码,以便如果所有元素==三个都打印一次Hi,并且如果有一个元素!=第三,它被打印了一次,一个函数可以这样做: one=driver.find_elements_by_x
one=driver.find_elements_by_xpath("any")
for two in one:
if two.text=='three'
print('Hello')
else:
print('Bye')
如何更改代码,以便如果所有元素==三个都打印一次Hi,并且如果有一个元素!=第三,它被打印了一次,一个函数可以这样做:
one=driver.find_elements_by_xpath("any")
def helloBye(one):
for two in one:
if two.text!="three":
print("Bye")
return
print("Hello")
return
helloBye(one)
函数可以做到这一点:
one=driver.find_elements_by_xpath("any")
def helloBye(one):
for two in one:
if two.text!="three":
print("Bye")
return
print("Hello")
return
helloBye(one)
你可以做一套,然后比较结果
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','not three'并将其转换为{'three','not three'}。然后你看它是否只有一个元素长,而这个元素是否为“三”
希望如果需要这个解释,它会有所帮助。你可以一组一组地做一个,然后比较结果
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','not three'并将其转换为{'three','not three'}。然后你看它是否只有一个元素长,而这个元素是否为“三”
希望如果需要这个解释,它会有所帮助。您可以使用任何
你可以使用任何
对不起,我以前不理解您的问题,这应该可以:
one=driver.find_elements_by_xpath("any")
i = 0
for two in one:
if two.text=='three'
i = i + 1
if and i==len(one)
print('Hello')
else:
print('Bye')
break
对不起,我以前不理解您的问题,这应该可以:
one=driver.find_elements_by_xpath("any")
i = 0
for two in one:
if two.text=='three'
i = i + 1
if and i==len(one)
print('Hello')
else:
print('Bye')
break
这可以通过多种方式实现: 这里我给你两种方法 第一个也是最简单的方法是维护计数器并检查计数器。这可以通过以下方式实现:
counter_three = 0
counter_not_three = 0
for two in one:
if two.text=='three':
if counter_three == 0:
counter_three = counter_three + 1
print('Hello')
else:
if counter_not_three == 0:
print('Bye')
counter_not_three = counter_not_three + 1
a = [{"text": "three"},{"text": "three"}, {"text": "three"}, {"text": "three"}, {"text": "two"}]
a_list = set(map(lambda x: x["text"], a))
for x in a_set:
if x == 'three':
print('Hello')
else:
print('Bye')
第二种方式如下:
counter_three = 0
counter_not_three = 0
for two in one:
if two.text=='three':
if counter_three == 0:
counter_three = counter_three + 1
print('Hello')
else:
if counter_not_three == 0:
print('Bye')
counter_not_three = counter_not_three + 1
a = [{"text": "three"},{"text": "three"}, {"text": "three"}, {"text": "three"}, {"text": "two"}]
a_list = set(map(lambda x: x["text"], a))
for x in a_set:
if x == 'three':
print('Hello')
else:
print('Bye')
这可以通过多种方式实现: 这里我给你两种方法 第一个也是最简单的方法是维护计数器并检查计数器。这可以通过以下方式实现:
counter_three = 0
counter_not_three = 0
for two in one:
if two.text=='three':
if counter_three == 0:
counter_three = counter_three + 1
print('Hello')
else:
if counter_not_three == 0:
print('Bye')
counter_not_three = counter_not_three + 1
a = [{"text": "three"},{"text": "three"}, {"text": "three"}, {"text": "three"}, {"text": "two"}]
a_list = set(map(lambda x: x["text"], a))
for x in a_set:
if x == 'three':
print('Hello')
else:
print('Bye')
第二种方式如下:
counter_three = 0
counter_not_three = 0
for two in one:
if two.text=='three':
if counter_three == 0:
counter_three = counter_three + 1
print('Hello')
else:
if counter_not_three == 0:
print('Bye')
counter_not_three = counter_not_three + 1
a = [{"text": "three"},{"text": "three"}, {"text": "three"}, {"text": "three"}, {"text": "two"}]
a_list = set(map(lambda x: x["text"], a))
for x in a_set:
if x == 'three':
print('Hello')
else:
print('Bye')
如果您使用all而不是any,则更清楚:
如果您使用all而不是any,则更清楚:
砰,我总是忘记任何事情。将我的答案带到下一个层次我对我的问题进行了编辑,以失去对意外事件的理解,因为我忘记了我们需要文本属性。当我发现自己的错误后意识到这一点时,我删除了我的评论。还是很好的回答!您的版本会多次打印“再见”或“你好”。我只需要一次。@keepomen再次测试了它,它只打印了一次。@Guy截图我做错什么了吗?轰,我总是忘记任何事情。将我的答案带到下一个层次我对我的问题进行了编辑,以失去对意外事件的理解,因为我忘记了我们需要文本属性。当我发现自己的错误后意识到这一点时,我删除了我的评论。还是很好的回答!您的版本会多次打印“再见”或“你好”。我只需要一次。@keepomen再次测试了它,它只打印了一次。@Guy屏幕截图我做错什么了吗?你的版本多次打印“再见”或“你好”。我只需要once@keepomen查看我编辑的解决方案。您不需要for二合一:循环。您的版本会多次打印Bye或Hello。我只需要once@keepomen查看我编辑的解决方案。您不需要二合一:循环。