文件上传selenium python

时间:2013-10-07 15:10:20

标签: python csv selenium selenium-webdriver

我试图通过将图像路径存储在.CSV文件中并通过csv.reader

读取来上传图像
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://google.com/"

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

    def test_browse(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_elements_by_xpath("//div[@class='addmore']/form/input[3]").send_keys(self.photo)#here is where I am trying to input the photo

在CSV文件中

我有一个像

这样的专栏
|__________photo__________|
|   /path to the image/   |

这样做会有效吗?我试过了。但我失败了。

1 个答案:

答案 0 :(得分:0)

对于路径,用户名,数据库等使用csv文件我没有问题...,为您提供了一个编辑它们的地方。至于设置文件上传,send_keys对我有用。

def set_resume_path(self, resume_path):
    '''
    Sets the path to the resume file.
    '''
    script = 'jQuery("#upload-resume-form").css({"visibility": "visible"})'
    self.driver.execute_script(script)
    WebDriverWait(self.driver, 2).until(lambda driver: driver.find_element_by_id\
                                        (self.locators['choose file button']).is_displayed())
    self.driver.find_element_by_id(self.locators['resume path input']).send_keys(resume_path)
    return self