繁体   English   中英

从另一个Python脚本调用python类

[英]Call python class from another Python script

我有2个Python脚本:1个是库,另一个是运行文件

库.py

from selenium import webdriver
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

'''  The purpose of this test is to test Google's email site '''

class GoogleTest(object):
''' Opens Firefox web browser and navigates to the declared website'''
def open_browser(self,url):
    global driver
    driver=webdriver.Firefox()
    time.sleep(3)
    driver.get(url)
def sign_in(self,emailaddr): # Signs in  to googles email
    driver.find_element_by_link_text('Sign in').click()
    email = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, "Email")))
    email.send_keys(emailaddr)
    driver.find_element_by_id('next').click()
    time.sleep(2)

run.py

from Library import GoogleTest
import os,sys

G=GoogleTest()

G.open_browser('https://www.google.com')
G.sign_in('emailssss@gmail.com')

我收到一条错误消息,指出sign_in需要2个参数,但只给出1个

我不明白。 我以为自我就是一个实例。 我没有使用它作为参数。

我使用适当的缩进方式更新了代码,以便使用方法和文档注释,并运行了python run.py命令,该命令成功执行了脚本。

您能否添加完整的堆栈跟踪信息? 如果错误提到问题确实出现,那么首先应该针对open_browser方法本身出现问题。

这是更新的类:

Library.py:

from selenium import webdriver
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

'''  The purpose of this test is to test Google's email site '''

class GoogleTest(object):
    ''' Opens Firefox web browser and navigates to the declared website'''
    def open_browser(self,url):
        global driver
        driver=webdriver.Firefox()
        time.sleep(3)
        driver.get(url)
    def sign_in(self,emailaddr): # Signs in  to googles email
        driver.find_element_by_link_text('Sign in').click()
        email = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, "Email")))
        email.send_keys(emailaddr)
        driver.find_element_by_id('next').click()
        time.sleep(2)

运行

from Library import GoogleTest
import os,sys

G=GoogleTest()

G.open_browser('https://www.google.com')
G.sign_in('emailssss@gmail.com')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM