-1

我正在尝试通过将图像路径存储在 .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/   |

那么这会奏效吗?我试过了。但我失败了。

4

1 回答 1

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
于 2013-10-07T18:42:06.583 回答