如何通过特定的浏览器作为争论。其错误提示:py.test:错误:无法识别的参数:--browser IE

问题描述 投票:0回答:1

我是Sellinium / Python的新手,几乎没有进行练习。我想使用Internet Explorer运行该程序,因此在命令提示符下,给出了以下命令:

py.test -s -v mainTest.py --browser IE

当我运行以上命令时,它显示错误为:

错误:用法:py.test [选项] [file_or_dir] [file_or_dir] [...]py.test:错误:无法识别的参数:--browser IEinifile:无``rootdir:C:\ Users \ Radha.Maravajhala \ PycharmProjects \ RadhaSelenium \ Tests

它不标识-浏览器争论。

我如何使用特定的浏览器运行我的程序,作为py.test命令的争论。请帮助。

我的代码:

mainTest.py

import os
import pytest
import unittest
from executionEngine.DriverScript import driverScript
from Utilities.Constants import Constants
from selenium import webdriver

class mainTest(driverScript):

def main(self):
    print("Main Test started...")
    print("Selenium webdriver Version: %s" % (webdriver.__version__))
    driver = driverScript('IE')
    driver.getbrowserInstance()

m = mainTest()
m.main()

DriverScript.py

import os
import pytest
import unittest

from pip._internal.configuration import Configuration
from selenium import webdriver
from selenium.webdriver import Ie
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.ie.options import Options
from selenium.webdriver.common.keys import Keys
#import selenium.webdriver
from Utilities import Constants

class driverScript():

def __init__(self,browser=None):

     if browser is None:
         browser = {}
     else:
         self.browser = browser
         print(self.browser)

     self.constants = Constants.Constants()

# Method to invoke browser
def getbrowserInstance(self):
    # create a new IE session
    print("Browser invoke started")

    if (self.browser=='IE'):

       capabilities = DesiredCapabilities.INTERNETEXPLORER
       print(capabilities["platform"])
       print(capabilities["browserName"])

       driver_location = self.constants.path_ie_driver

       os.environ["webdriver.ie.driver"] = driver_location

       ie_options = Options()
       ie_options.ignore_protected_mode_settings = True

       driver = webdriver.Ie(driver_location, options=ie_options)

       print("Browser is Invoked")
       driver.get("http://www.amazon.co.uk")
       driver.quit()

Constants.py:

 class Constants():

 #Driver Locations
 path_ie_driver = "C:\\Selenium\\Drivers\\IEDriverServer.exe"

confest.py

enter code here

class Constants():

#Driver Locations
path_ie_driver = "C:\\Selenium\\Drivers\\IEDriverServer.exe"
python internet-explorer browser arguments command
1个回答
0
投票

似乎您对传递python脚本的参数有些误解。

如果要使用多个浏览器运行脚本,则需要在代码中使用多个Web驱动程序。在上面的代码中,我可以看到您仅使用IE Web驱动程序。

另一件事,我没有看到您在哪里检查并使用从命令行传递的参数。

我建议您使用IF..elif条件,并尝试检查其中的命令行参数。根据参数的值,您可以尝试为任何特定的Web浏览器执行代码段。

示例:

#!/usr/bin/python

import sys, getopt

def main(argv):
   inputfile = ''
   outputfile = ''
   try:
      opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
   except getopt.GetoptError:
      print 'test.py -i <inputfile> -o <outputfile>'
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print 'test.py -i <inputfile> -o <outputfile>'
         sys.exit()
      elif opt in ("-i", "--ifile"):
         inputfile = arg
      elif opt in ("-o", "--ofile"):
         outputfile = arg
   print 'Input file is "', inputfile
   print 'Output file is "', outputfile

if __name__ == "__main__":
   main(sys.argv[1:])

参考:

Python - Command Line Arguments

© www.soinside.com 2019 - 2024. All rights reserved.