【问题标题】:Selenium click a button if the parameter is true如果参数为 true,Selenium 单击按钮
【发布时间】:2016-09-27 02:30:18
【问题描述】:

我想创建一个带参数的函数,如果参数为 True,则单击按钮,否则单击否。我可以用这个吗?

def buttonClick(self, Button):
    if Button == True:
        self.driver.find_element_by_id('button').click

【问题讨论】:

  • 是的,您可以使用它。谢谢

标签: python selenium


【解决方案1】:

我想解决两个主要问题:

  • 您可以避免拥有== True 部分
  • 您没有调用click 方法 - 添加()

固定版本:

def buttonClick(self, should_click_button):
    if should_click_button:
        self.driver.find_element_by_id('button').click()

示例用法:

instance = MyClass()
instance.buttonClick(True)
instance.buttonClick(False)

您还可以设置参数的默认值:

def buttonClick(self, should_click_button=False):
    if should_click_button:
        self.driver.find_element_by_id('button').click()

现在,如果您不需要点击按钮,只需不要传递参数:

instance = MyClass()
instance.buttonClick(True)
instance.buttonClick()

【讨论】:

  • 对不起,我还是编程新手。我需要在 should_click_button 中输入什么参数才能使用它?
  • @ChiragVerma 用一些示例更新了答案,希望对您有所帮助。
猜你喜欢
  • 2013-12-24
  • 2019-08-19
  • 1970-01-01
  • 2013-05-07
  • 2021-05-04
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
相关资源
最近更新 更多