从另一个Python脚本调用python类

时间:2017-01-07 01:07:18

标签: python selenium selenium-webdriver

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

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)

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个参数

我不明白。我认为 self 是一个实例。我没有将它用作参数。

1 个答案:

答案 0 :(得分:0)

我使用适当的方法和文档注释缩进更新了代码,并运行了成功执行脚本的命令RedirectMatch ^/phpmyadmin$ http://example.com/404

您能否添加完整的堆栈跟踪?如果错误提到问题确实出现,那么首先它应该出现在python run.py方法本身。

以下是更新的课程:

Library.py:

open_browser

Run.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)