1

I have a script to upload a file to the site, nothing happens when i launch it, I thought the problem was in the compatibility of chromedriver and the browser, but the driver update did not give a result, the script does not issue errors, launching on pycharm outputs "Process finished with exit code 0", i used logging, it shows that the connection is normal, the path according to the site, it also seems to be spelled out correctly. Tell me please what the problem may be.

from seleniumbase import BaseCase
import time
from datetime import datetime
import logging
import os
from seleniumbase import get_driver
driver = get_driver("chrome")
file_path = os.path.abspath("file.xlsx")

logging.basicConfig(level=logging.INFO, filename="py_log.log", format='%(asctime)s - %(levelname)s - %(message)s')
logging.info('The script is starting to run')

class MyTestClass(BaseCase):
    def test_visible_upload(self):
        self.driver = driver
        self.open("https://site/")
        #time.sleep(2)
        self.refresh()
        #time.sleep(2)
        self.refresh()
        #time.sleep(2)
        self.type('#ke-form-field-input-0', 'login')
        #time.sleep(1)
        self.type('#ke-form-field-input-1', 'password')
        #time.sleep(5)
        self.click('body > kn-app > kn-auth > kn-auth-sign-in > kn-auth-sign-in-form > form > button')
        title = self.get_title()
        print(title)`
#Here tried to get site`s title but it didn`t work
`self.choose_file('#kpp-app > div.body > div > div.content-side > div > div.content > div > div.cs-section > div > div > label > div > input[type=file]', file_path)`
#File upload string.

i expect not empty result when launch it

1 Answers1

0

SeleniumBase has two good examples of file uploading that you can run from the SeleniumBase examples/ folder if you cloned a copy:

import os
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)


class FileUploadButtonTests(BaseCase):
    def test_file_upload_button(self):
        self.open("https://seleniumbase.io/w3schools/file_upload")
        self.click("button#runbtn")
        self.switch_to_frame("iframeResult")
        zoom_in = 'input[type="file"]{zoom: 1.6;-moz-transform: scale(1.6);}'
        self.add_css_style(zoom_in)
        self.highlight('input[type="file"]')
        dir_name = os.path.dirname(os.path.abspath(__file__))
        my_file = "screenshot.png"
        file_path = os.path.join(dir_name, "example_logs/%s" % my_file)
        self.assert_attribute("#myFile", "value", "")
        self.choose_file('input[type="file"]', file_path)
        self.assert_attribute("#myFile", "value", "C:\\fakepath\\%s" % my_file)
        self.highlight('input[type="file"]')

and

import os
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)


class FileUpload(BaseCase):
    def test_show_file_choosers(self):
        self.open("https://seleniumbase.io/apps/img_upload")
        self.wait_for_element('img[alt="ImgBB"]')
        choose_file_selector = 'input[type="file"]'
        uploaded_image = "#anywhere-upload-queue li.queue-item"
        self.assert_element_not_visible(choose_file_selector)
        self.show_file_choosers()
        self.highlight(choose_file_selector)
        self.assert_element(choose_file_selector)
        self.assert_attribute(choose_file_selector, "value", "")
        self.assert_element_not_visible(uploaded_image)
        dir_name = os.path.dirname(os.path.abspath(__file__))
        my_file = "screenshot.png"
        file_path = os.path.join(dir_name, "example_logs/%s" % my_file)
        self.choose_file(choose_file_selector, file_path)
        if self.browser != "safari":
            seen_path = "%s\\%s" % ("C:\\fakepath", my_file)
            self.assert_attribute(choose_file_selector, "value", seen_path)
        self.demo_mode = True
        self.assert_element(uploaded_image)

In the second one, there is a special self.show_file_choosers() method that reveals hidden file upload input fields on a website that has those. If that input field is visible, then you can use self.choose_file('input[type="file"]', file_path) to upload a file.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48