如何将参数添加到自动生成的 Python Selenium 脚本
How to add parameters to an autogenerated Python Selenium script
我是 Selenium 的新手,正在尝试了解如何将参数传递给 selenium 脚本。
通常如果我用这样的参数调用 python 脚本
python myprogram.py 我的参数
从命令行,我可以在中查找参数
sys.argv[1]
(假设我导入了 sys)
自动生成的 selenium 脚本如下所示:
(我已经尝试添加 sys.argv[1] 了)
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import sys
class ParaN(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.de/"
self.verificationErrors = []
self.accept_next_alert = True
def test_para(self):
driver = self.driver
driver.get(self.base_url + "?q=" + sys.argv[1])
print(driver.current_url )
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
此示例应该调用 google 并将参数作为搜索词。
但是 unittest.main() 不接受参数,我不明白 unittest.main() 运行时会发生什么。
为此添加参数的最佳方法是什么?
unittest.main()
解析您在命令行上传递的所有内容:
A command-line program that loads a set of tests from module and runs
them; this is primarily for making test modules conveniently
executable.
一种替代方法是使用 TestLoader
。替换:
unittest.main()
与:
runner = unittest.TextTestRunner()
suite = unittest.TestLoader().loadTestsFromTestCase(ParaN)
runner.run(suite)
我是 Selenium 的新手,正在尝试了解如何将参数传递给 selenium 脚本。
通常如果我用这样的参数调用 python 脚本 python myprogram.py 我的参数 从命令行,我可以在中查找参数 sys.argv[1] (假设我导入了 sys)
自动生成的 selenium 脚本如下所示: (我已经尝试添加 sys.argv[1] 了)
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import sys
class ParaN(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.de/"
self.verificationErrors = []
self.accept_next_alert = True
def test_para(self):
driver = self.driver
driver.get(self.base_url + "?q=" + sys.argv[1])
print(driver.current_url )
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
此示例应该调用 google 并将参数作为搜索词。
但是 unittest.main() 不接受参数,我不明白 unittest.main() 运行时会发生什么。
为此添加参数的最佳方法是什么?
unittest.main()
解析您在命令行上传递的所有内容:
A command-line program that loads a set of tests from module and runs them; this is primarily for making test modules conveniently executable.
一种替代方法是使用 TestLoader
。替换:
unittest.main()
与:
runner = unittest.TextTestRunner()
suite = unittest.TestLoader().loadTestsFromTestCase(ParaN)
runner.run(suite)