在硒中循环

发布于 2024-10-31 05:02:34 字数 93 浏览 1 评论 0原文

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

I recorded one script using Selenium IDE which contain clicking on a link and now i want to add loop to run same script multiple time, for this i am converting script to python but unable to add loop.Please help me in this regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

源来凯始玺欢你 2024-11-07 05:02:34

以下是直接来自 selenium 文档的一些文本:

数据驱动测试:
数据驱动测试是指使用不同的数据多次使用相同的测试(或多个测试)。这些数据集通常来自外部文件,即 .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 文档

谨致问候,

Paulo Bueno。

Heres some text direct from selenium docs:

Data Driven Testing:
Data Driven Testing refers to using the same test (or tests) multiple times with varying data. These data sets are often from external files i.e. .csv file, text file, or perhaps loaded from a database. Data driven testing is a commonly used test automation technique used to validate an application against many varying input. When the test is designed for varying data, the input data can expand, essentially creating additional tests, without requiring changes to the test code.

# 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))

Hope it helps. More info at: Selenium Documentation

Best Regards,

Paulo Bueno.

倥絔 2024-11-07 05:02:34

尝试类似于此示例的循环,使用“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()

Try a loop similar to this example using "for x in range (0,5):" to set the number of times you wish it to iterate.

    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()
余生一个溪 2024-11-07 05:02:34

我在一些我几乎没有信息的情况下尝试过这个:

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

I tried this for some situations that I have little information:

list = [''' list containing all items ''']
index = 0
while True:
    try:
        # do what you want with list[index]
        index += 1
    except:
        # index exception occured 
        break
荒路情人 2024-11-07 05:02:34

在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(){
}
}

In java you can do this as below:

# 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(){
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文