我正在使用值列表循环播放此脚本。我有一个要使用的所有.send_keys
值的excel文档。我打算利用openpyxl在python中提取一个列表。我也可以在python中定义列表,并且在需要时不使用openpyxl。
目前,我只需要复制粘贴两次整个脚本,只是因为我希望此脚本与这些不同的发送键变量一起运行。
driver.find_element_by_name("t1st__TBOX").send_keys("619272")
driver.find_element_by_name("t1st__TBOX").send_keys("894323")
我有约100个唯一变量,因此我正在寻找.send_keys
仅引用列表并注入值并重新运行脚本的方法。
如何循环播放此脚本,使其仅引用具有多个send_keys变量的列表?
任何对可能有用的东西的见解都是很棒的!
# -*- 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
from selenium.webdriver.support import expected_conditions as EC
import unittest, time, re
class Test1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "https://www.website.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_1(self):
driver = self.driver
driver.get("https://website.com/")
driver.implicitly_wait(3)
driver.find_element_by_xpath("//div[5]/div/div[2]/img").click()
driver.implicitly_wait(3)
driver.switch_to.window(driver.window_handles[1])
seq = driver.find_elements_by_tag_name('iframe')
print("Number of frames present in the web page are: ", len(seq))
driver.switch_to.default_content()
for x in range(2):
try:
driver.switch_to.frame(x)
driver.find_element_by_xpath("//tr[3]/td").click()
except:
print("It's not: ", x)
continue
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::td[5]").click()
driver.find_element_by_name("t1st__TBOX").clear()
driver.find_element_by_name("t1st__TBOX").send_keys("619272")
print("Number of frames present in the web page are: ", len(seq))
for x in range(2):
try:
driver.switch_to.frame(x)
driver.find_element_by_xpath("//span[@id='p1st__PDLink']/img").click()
except:
print("It's not: ", x)
continue
print("Number of frames present in the web page are: ", len(seq))
driver.switch_to.default_content()
for x in range(2):
try:
driver.switch_to.frame(x)
driver.find_element_by_xpath("//tr[@id='p1st__PD_MIRow_1_']/td[3]").click()
except:
print("It's not: ", x)
continue
print("Number of frames present in the web page are: ", len(seq))
driver.switch_to.default_content()
for x in range(2):
try:
driver.switch_to.frame(x)
driver.find_element_by_xpath("//font/font/input").click()
except:
print("It's not: ", x)
continue
driver.find_element_by_xpath("//font/font/input").click()
driver.find_element_by_link_text("Q619272").click()
driver.find_element_by_link_text("Email").click()
driver.find_element_by_xpath("//*[@id='null']/option[2973]").click()
# ERROR: Caught exception [ERROR: Unsupported command [addSelection | id=null | label=Doe, John([email protected])]]
driver.find_element_by_xpath(
"(.//*[normalize-space(text()) and normalize-space(.)='Email this Quote'])[1]/following::input[2]").click()
Select(driver.find_element_by_name("FORMAT")).select_by_visible_text("Text")
driver.find_element_by_link_text("Send").click()
driver.close()
##Round 2##
driver = webdriver.Chrome()
driver.get("https://website.com")
driver.implicitly_wait(3)
driver.find_element_by_xpath("//div[5]/div/div[2]/img").click()
driver.implicitly_wait(3)
driver.switch_to.window(driver.window_handles[1])
seq = driver.find_elements_by_tag_name('iframe')
print("Number of frames present in the web page are: ", len(seq))
driver.switch_to.default_content()
for x in range(2):
try:
driver.switch_to.frame(x)
driver.find_element_by_xpath("//tr[3]/td").click()
except:
print("It's not: ", x)
continue
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::td[5]").click()
driver.find_element_by_name("t1st__TBOX").clear()
driver.find_element_by_name("t1st__TBOX").send_keys("894323")
print("Number of frames present in the web page are: ", len(seq))
for x in range(2):
try:
driver.switch_to.frame(x)
driver.find_element_by_xpath("//span[@id='p1st__PDLink']/img").click()
except:
print("It's not: ", x)
continue
print("Number of frames present in the web page are: ", len(seq))
driver.switch_to.default_content()
for x in range(2):
try:
driver.switch_to.frame(x)
driver.find_element_by_xpath("//tr[@id='p1st__PD_MIRow_1_']/td[3]").click()
except:
print("It's not: ", x)
continue
print("Number of frames present in the web page are: ", len(seq))
driver.switch_to.default_content()
for x in range(2):
try:
driver.switch_to.frame(x)
driver.find_element_by_xpath("//font/font/input").click()
except:
print("It's not: ", x)
continue
driver.find_element_by_xpath("//font/font/input").click()
driver.find_element_by_link_text("Q894323").click()
driver.find_element_by_link_text("Email").click()
driver.find_element_by_xpath("//*[@id='null']/option[2973]").click()
# ERROR: Caught exception [ERROR: Unsupported command [addSelection | id=null | label=doe, jane([email protected])]]
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email this Quote'])[1]/following::input[2]").click()
Select(driver.find_element_by_name("FORMAT")).select_by_visible_text("Text")
driver.find_element_by_link_text("Send").click()
driver.close()
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException as e:
return False
return True
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException as 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()
参考方案
您需要阅读setUp
中的excel并创建一个列表/元组。然后在该列表的test_1
循环中,并将test_1
的所有当前内容放入该循环中。那应该工作。
class Test1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "https://www.website.com/"
self.verificationErrors = []
self.accept_next_alert = True
# Read excel here to create a list,
self.my_keys = ('809767', `797978`, ..., ) # or just copy paste the contents of excel here and make a list/tuple yourself.
def test_1(self):
for key in self.my_keys:
# all the previous code of test_1 here
...
driver.find_element_by_name("t1st__TBOX").clear()
driver.find_element_by_name("t1st__TBOX").send_keys(key) # use varible key here to send different key each loop
...
# all the previous code of test_1 here
这应该足够了,可能需要进行一些更改,因为在test_1
之后我仍然无法理解方法的用途,因此我从未见过您调用它们。同样,此测试可能会运行很长时间,具体取决于您必须发送的密钥数量。
在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…
Python-crontab模块 - python我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…
Python:检查是否存在维基百科文章 - python我试图弄清楚如何检查Wikipedia文章是否存在。例如,https://en.wikipedia.org/wiki/Food 存在,但是https://en.wikipedia.org/wiki/Fod 不会,页面只是说:“维基百科没有此名称的文章。”谢谢! 参考方案 >>> import urllib >>> prin…
Python Pandas导出数据 - python我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…
Python:find_element_by_css_selector - python我正在尝试使用webdriver单击登录按钮<a class="login-btn" href="javascript:;" data-bind="click:loginSection.loginClick"> <span class="btn-text">…