【问题标题】:How to pass specific browser as arguement. Its errorring : py.test: error: unrecognized arguments: --browser IE如何将特定浏览器作为参数传递。它的错误:pytest:错误:无法识别的参数:--browser IE
【发布时间】:2020-05-02 15:58:44
【问题描述】:

我是 Sellinium/Python 的新手,并且很少进行练习。 我想用 Internet Explorer 运行这个程序,所以在命令提示符下,我给出了以下命令:

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

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

错误:用法:py.test [options] [file_or_dir] [file_or_dir] [...] py.test:错误:无法识别的参数:--browser IE inifile:无`` 根目录:C:\Users\Radha.Maravajhala\PycharmProjects\RadhaSelenium\Tests

它不是识别 --browser 争论。

如何使用特定浏览器运行我的程序作为 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()

    elif (self.browser=='Chrome'):

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

       driver_location = self.constants.path_chrome_driver

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

       driver = webdriver.Chrome(driver_location)

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

常量.py:

 class Constants():

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

confest.py

class Constants():

import pytest
from executionEngine.DriverScript import driverScript

# Send request variable to the fixture
# Browser value will be set from request.config.getoption (from command 
#                                                           prompt)
@pytest.yield_fixture(scope="class")
 def invoke_browser(request,browser):
     wdf = driverScript.getbrowserInstance(browser)
     driver = wdf.getbrowserInstance()

# Set class attribute and assign the variable
    if request.cls is not None:
       request.cls.driver = driver
       yield driver
 
driver.quit()

创建 2 个解析器以从命令提示符获取值

def pytest_addoption(parser):
    parser.addoption("--browser")

返回参数值

    @pytest.fixture(scope="session")
def browser(request):
    return request.config.getoption("--browser")

【问题讨论】:

  • 您在这里显示的只是一个命令。你的python脚本里面有什么?如果您发布脚本示例会更好。它可以帮助我们更好地理解问题。如果您不传递参数,那么您的脚本是否可以正常运行?让我们知道您指的是哪些文档。我们将尝试检查它以找到如何正确实施它。
  • 感谢您的回复迪帕克吉。我已经用我的代码更新了通话。请看一看。
  • 我可以看到您在代码中传递了 IE 驱动程序的路径。之后,您不需要将浏览器作为参数传递,因为您在代码中使用了哪个驱动程序。它将启动该特定浏览器。在没有任何参数的情况下执行 python 脚本时是否遇到任何错误?
  • Deepak ji,没有参数,脚本运行良好,网站在 IE 中打开,正如预期的那样。不过,我打算用 Chrome 和 Edge 扩展脚本。当我使用这两个浏览器进行扩展时,我可能需要选择一个特定的浏览器来从命令提示符运行。这就是为什么我试图将浏览器作为参数传递并查看它是如何工作的。脚本适用于单个浏览器。但是当我用 Chrome 和 Edge 扩展它时,我该如何管理它?这就是为什么我试图将它作为参数传递。请指导我如何处理多个浏览器,我可以在运行时选择特定的浏览器。

标签: python internet-explorer browser arguments command


【解决方案1】:

看起来您对传递 python 脚本的参数有一些误解。

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

另外,我没有看到你在哪里检查和使用你从命令行传递的参数。

我建议您使用 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

【讨论】:

  • 我很抱歉。已为 IE 和 Chrome 编写代码。在 Stackoverflow 编辑器中复制时,似乎缺少 elif 部分。另外 - confest.py 正确复制。关于检查和使用参数的查询,下面的代码应该从命令行获取参数:
  • 我很抱歉。已为 IE 和 Chrome 编写代码。在 Stackoverflow 编辑器中复制时,似乎缺少 elif 部分。另外 - confest.py 正确复制。关于您在哪里检查和使用争论的查询,请参阅更新的 confest.py 。我尝试使用以下命令从命令提示符执行此操作: py.test -s -v mainTest.py --browser Chrome 它仍然显示:错误:用法:py.test [options] [file_or_dir] [file_or_dir] [.. .] py.test: error: unrecognized arguments: --browser Chrome inifile: None rootdir: C:\Users\Radha.Maravajhala\PycharmProjects\RadhaSelenium\Tests
  • 请忽略这个问题。现在已解决。 :)
  • 感谢您通知我们问题已解决。我建议您将您的解决方案发布为该线程的答案,并尝试在 48 小时后将您自己的答案标记为该问题的可接受答案,当它可以标记时。它可以在未来帮助其他社区成员解决类似的问题。感谢您的理解
猜你喜欢
  • 1970-01-01
  • 2022-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 2021-01-14
相关资源
最近更新 更多