【问题标题】:Python Selenium LoopPython 硒循环
【发布时间】:2018-12-05 03:47:29
【问题描述】:

我希望使用值列表循环此脚本。我有一个 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 仅引用一个列表并注入值并重新运行脚本。

如何循环这个脚本,让它只引用一个包含多个 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()

【问题讨论】:

  • 你已经知道该怎么做了,在python中创建一个列表,或者从读取excel中获取值,然后循环进去。这是你的整个脚本吗,只有一/两行?
  • 嗨 Vaibhav 从概念上讲,我了解我需要做什么,但我不确定如何编写该循环。也不太清楚如何使用 openpyxl 从 excel 中提取列表并将其用作参考点来编写所需的代码,但是一旦我弄清楚我想的循环,我就会弄清楚这一点。 :) 现在,如果我能想出一种方法来循环通过我认为可行的预定义列表。
  • def test_1的整个过程需要对每个key都重复一遍吗?对不起,我很久以前使用过 selenium,我不太记得它是如何工作的。
  • 没错。

标签: python html css selenium xpath


【解决方案1】:

您需要阅读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之后的方法的目的,我从来没有看到你调用它们。此外,此测试可能会运行很长时间,具体取决于您必须发送的密钥数量。

【讨论】:

    猜你喜欢
    • 2018-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2017-08-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多