【问题标题】:How to execute testcases through Python Class using Selenium如何使用 Selenium 通过 Python 类执行测试用例
【发布时间】:2022-01-03 17:55:38
【问题描述】:

所以我正在尝试使用 python 和 selenium 构建一个机器人 这是我的代码

from selenium import webdriver
import os
import time

class InstagramBot:

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Chrome(executable_path='./chromedriver.exe')
        self.driver.get('https://www.Instagram.com/')

问题是当我尝试 python bot.py 时什么都不会发生 我也试过 py bot.py 它没有抛出任何错误,但命令真的什么都不做

请有人帮我找出问题所在???

我在课堂外尝试过 driver = webdriver.chrome ... 并且它可以工作,但是当我将它放在 InstagramBot 课程中时它不会工作

我正在使用 python3.6.5 我也尝试过其他python版本 它没有帮助

【问题讨论】:

  • 这是整个代码吗?你还记得初始化你的类来调用函数吗?例如i = InstagramBot()。添加另一种方法来运行你的机器人可能会更好def run(self): ... 然后你可以调用InstagramBot().run()
  • 是的,先生,它不完整,但当我找出问题所在时就会出现

标签: python selenium selenium-webdriver instagram python-class


【解决方案1】:

你已经够近了。由于您已经定义了 Class,现在您只需要创建一个实例,以便执行构造函数从 main 调用它,如下所示:

from selenium import webdriver
import os
import time

class InstagramBot:

    def __init__(self, username, password):
    self.username = username
    self.password = password
    self.driver = webdriver.Chrome(executable_path='./chromedriver.exe')
    self.driver.get('https://www.Instagram.com/')

InstagramBot("naa-G", "naa-G")

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多