如何在Python Selenium脚本中应用循环?

0 投票
1 回答
2610 浏览
提问于 2025-04-15 14:57

我正在尝试在这个脚本中使用for循环,但遇到了很多困难。我想要遍历一个存储在csv格式中的子域名列表(也就是说:一列有20个子域名),并为每个子域名打印出html代码。它们都有相同的源域名。谢谢!

#Python 2.6
from selenium import selenium
import unittest, time, re, csv, logging

class Untitled(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox", "http://www.SourceDomain.com")
        self.selenium.start()

    def test_untitled(self):
        sel = self.selenium
        sel.open("/dns/www.subdomains.com.html")
        sel.wait_for_page_to_load("30000")
        html = sel.get_html_source()
        print html

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

1 个回答

3
#Python 2.6
from selenium import selenium
import unittest, time, re, csv, logging

class Untitled(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox", "http://www.SourceDomain.com")
        self.selenium.start()

    def test_untitled(self):
        sel = self.selenium
        spamReader = csv.reader(open('your_file.csv'))
        for row in spamReader:
            sel.open(row[0])
            sel.wait_for_page_to_load("30000")
            print sel.get_html_source()

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
import urllib2, csv

def fetchsource(url):
    page = urllib2.urlopen(url)
    source = page.read()
    return source

fooReader = csv.reader(open('your_file.csv'))
for url in fooReader:
    print fetchsource(url)

顺便提一下,注意到这个脚本不需要放在一个单元测试的测试用例里面。更棒的是,对于这么简单的任务,你其实不需要用到selenium(至少乍一看是这样)。

试试这个:

撰写回答