python+selenium+wewbdriverアップロードファイル


  • inputタイプ
  • 1、webdriverを使うsend_keysメソッド:
    def test_uploadFileBySendKeys(self):
        url = "file:///Users/doudou/Desktop/six.html"
        self.driver.get(url)
    
        try:
            #          
            wait = WebDriverWait(self.driver,10,0.2)
            #                     
            wait.until(EC.element_to_be_clickable((By.ID,'file')))
        except TimeoutException as e:
            print(e)
        except NoSuchElementException as e:
            print(e)
        except Exception as e:
            print(e)
        else:
            #      "file"     
            fileBox = self.driver.find_element_by_id('file')
            #                      "/Users/doudou/Desktop/six.html"
            fileBox.send_keys("/Users/doudou/Desktop/six.html")
            time.sleep(3)
            # #     
            self.driver.find_element_by_id("filesubmit").click()
  • 非Inputタイプ