1

単一のログイン資格情報でコードを使用しています。Python webdriver を使用して、複数のログイン資格情報を使用する必要があります。以下のコードでcsvファイル(複数のログイン資格情報)を呼び出す方法を教えてください。

 from selenium import webdriver
 from selenium.webdriver.common.by import By
 from selenium.webdriver.support.ui import Select
 from selenium.common.exceptions import NoSuchElementException
 import unittest, time, re

 class Browse(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://careergrab.ionface.com/"
        self.username=['senthil@gmail.com','senthil@gmail.com']
        self.password=['12345','12345']
        self.verificationErrors = []

    def test_browse(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_css_selector("img[alt=\"img\"]").click()
        driver.find_element_by_link_text("Career Grab").click()
        driver.find_element_by_class_name("browse").click()
        driver.find_element_by_id("id_identification").clear()
        driver.find_element_by_id("id_identification").send_keys(self.username)
        for index in range(len(self.password)):
        driver.find_element_by_id("id_password").clear()
        driver.set_page_load_timeout
        driver.find_element_by_id("id_password").send_keys(self.password)
        driver.find_element_by_css_selector("input.login-button").click()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
4

1 に答える 1

2

解決策を見つけました。ファイル test.csv を作成する必要があります。以下のコードをコピーして貼り付けます。それは私のために働いた。理解を深めるためにコードを読んでください:)

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import Select
    from selenium.common.exceptions import NoSuchElementException
    import unittest, time, re

    class Browse(unittest.TestCase):
    def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(30)
    self.base_url = "http://careergrab.ionface.com/"

    filename = 'test.csv'
    line_number = 1
    with open(filename, 'rb') as f:
        mycsv = csv.reader(f)
        mycsv = list(mycsv)
        self.username=mycsv[line_number][0]
        self.password=mycsv[line_number][1]
        self.verificationErrors = []

def test_browse(self):
    driver = self.driver
    driver.get(self.base_url + "/")
    driver.find_element_by_css_selector("img[alt=\"img\"]").click()
    driver.find_element_by_link_text("Career Grab").click()
    driver.find_element_by_class_name("browse").click()
    driver.find_element_by_id("id_identification").clear()
    driver.find_element_by_id("id_identification").send_keys(self.username)
    for index in range(len(self.password)):
    driver.find_element_by_id("id_password").clear()
    driver.set_page_load_timeout
    driver.find_element_by_id("id_password").send_keys(self.password)
    driver.find_element_by_css_selector("input.login-button").click()

def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException, e: return False
    return True

def tearDown(self):
    self.driver.quit()
    self.assertEqual([], self.verificationErrors)
于 2013-01-16T20:54:59.903 に答える