Python Selenium 循环

标签 python html css selenium xpath

我希望使用值列表循环此脚本。我有一个 Excel 文档,其中包含我想要使用的所有 .send_keys 值。我打算利用 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 仅引用列表并注入(inject)值并重新运行脚本。

如何循环此脚本,使其仅引用具有多个 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(johndoe@abcde.com)]]
    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(janedoe@abcde.com)]]
    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 之后我仍然无法理解方法的用途,我从未见过您调用它们。此外,此测试可能会运行很长时间,具体取决于您必须发送的 key 数量。

关于Python Selenium 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624889/

相关文章:

python - 等同于 python 日期时间

javascript - 选择一个选项后取消突出显示下拉菜单

javascript - 通过 GUI 生成动态 HTML 表单

jquery - 具有 3 列的响应式设计

html - 尝试垂直线

python - 是否可以使用 Python 和 cx_Oracle 进行空间查询?

python - 为什么 statsmodels 和 R 之间的逻辑回归结果不同?

python - 特定格式的字符串

html - 具有相同高度的 Flex 盒子内部元素

javascript - 表中的第 N 个 child 不工作 Javascript