在硒中循环

时间:2011-04-08 12:43:35

标签: selenium

我使用Selenium IDE录制了一个脚本,其中包含单击链接,现在我想添加循环以多次运行相同的脚本,为此我将脚本转换为python但无法添加循环。请在这方面帮助我

4 个答案:

答案 0 :(得分:2)

直接来自selenium docs的一些文字:

  

数据驱动测试:   数据驱动测试是指使用不同的数据多次使用相同的测试(或测试)。这些数据集通常来自外部文件,即.csv文件,文本文件,或者可能从数据库加载。数据驱动测试是一种常用的测试自动化技术,用于根据许多不同的输入验证应用程序。当测试是针对不同的数据而设计的时,输入数据可以扩展,基本上可以创建额外的测试,而无需更改测试代码。

# Collection of String values
source = open("input_file.txt", "r")
values = source.readlines()
source.close()
# Execute For loop for each String in the values array
for search in values:
    sel.open("/")
    sel.type("q", search)
    sel.click("btnG")
    sel.waitForPageToLoad("30000")
    self.failUnless(sel.is_text_present("Results * for " + search))

希望它有所帮助。更多信息:Selenium Documentation

最诚挚的问候,

Paulo Bueno。

答案 1 :(得分:0)

使用“for x in range(0,5):”来尝试类似于此示例的循环,以设置您希望迭代的次数。

    def test_py2webdriverselenium(self):
        for x in range(0,5):
            driver = self.driver
            driver.get("http://www.bing.com/")
            driver.find_element_by_id("sb_form_q").click()
            driver.find_element_by_id("sb_form_q").clear()
            driver.find_element_by_id("sb_form_q").send_keys("testing software")
            driver.find_element_by_id("sb_form_go").click()
            driver.find_element_by_link_text("Bing").click()

答案 2 :(得分:0)

我在某些情况下尝试了这个,但我没有什么信息:

list = [''' list containing all items ''']
index = 0
while True:
    try:
        # do what you want with list[index]
        index += 1
    except:
        # index exception occured 
        break

答案 3 :(得分:0)

在java中你可以这样做:

# import packages or classes

public class testClassName(){

before test Methods(){
}

@Test
public void testMethod(){
    for(int i =0, i<=5, i++){
        WebElement element = driver.findElementById("link_ID");
        element.click();
        waitForPageLoaded(5);
    }
}

after Test Method(){
}
}