2017-09-27 3 views
0

나는 많은 사람들의 조언을 읽고 Windows를 사용하지 않고 내 파일의 경로를 직접 작성합니다. 심지어 같은 포럼에서 발견 된 몇 가지 명령을 사용하려고 시도 :Python Selenium Windows를 통해 파일 업로드 업로드

swicthTo() 
switch_to_window() 
window_handles 

그러나 아직 해결책을 찾지 못했습니다. 내 주요 문제는 내가 직접 내 파일의 경로를 보낼 수있는 공간이 없다는 것입니다 (아래 그림 참조, 경로를 소개하는 공간은 회색입니다). 그러나 "찾아보기"를 클릭하고 윈도우 업 로더 :

enter image description here

당신은 내가 업로드 윈도우의 윈도우로 전환 내 헛소리를 소개하는 방법을 알고 계십니까?

browse=wait(".//*[@id='fileinput']") #open the window upload 
browse.click() 
time.sleep(1) 


def handle_dialog(element_initiating_dialog, dialog_text_input): 
    upload_dialog = driver.switch_to_active_element 
    print (upload_dialog) 
    upload_dialog.send_keys(dialog_text_input) 
    upload_dialog.send_keys(selenium.webdriver.common.keys.Keys.ENTER) # the ENTER key closes the upload dialog, other thread exits 

handle_dialog(browse, "foobar.txt") 

내가 창문을 발견하고 나는 인쇄 할 때 나는이 개체가 :

는 난이 방법으로 시도

We are already in the Location->Details Page <bound method WebDriver.switch_to_active_element of <selenium.webdriver.ie.webdriver.WebDriver (session="3e725bb7-40a7-452a-ad90-9cca1d41296a")>> 

하지만이 때 이후

가 send_keys 나는이 오류가 발생을 할를 :

Traceback (most recent call last): File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 149, in handle_dialog(browse, "foobar.txt") File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 145, in handle_dialog upload_dialog.send_keys(dialog_text_input) AttributeError: 'function' object has no attribute 'send_keys'

부분적으로 해결책을 찾았습니다. 마치 창 업로드가 경고 인 것처럼 행동하게 만듭니다. o이 방법으로 :

browse=wait(".//*[@id='fileinput']") 
browse.click() 
time.sleep(1) 

upload_dialog = driver.switch_to_alert() 
print (upload_dialog) 

upload_dialog.send_keys("C:\\Users\\carlo.agresta\\Desktop\\V4LabCredentials.csv") 

이제 내 문제는 내가 동의하고 창을 닫을 수 없다는 것입니다. 조언이 있습니까? 사전에 너무 많은

감사

+0

. 왜냐하면 셀렌이 제어 할 수없는 네이티브 OS 창으로 이동하기 때문입니다. 당신이해야 할 일은'browse.click()'을'browse.send_keys ("<전체 파일 경로">)로 변경하는 것입니다. –

+0

예, 알고 있습니다 만 다른 옵션이 없으므로 텍스트 상자가 회색으로 표시됩니다. 내 파일을 선택하는 다른 방법이 없습니다. 지금까지 대화 창을 열고 내 경로를 소개 할 수 있습니다. 커맨드 .accept()가 작동하지 않기 때문에 받아 들일 수있는 방법이 마음에 들지 않습니다. 단지 "열림"과 "취소"두 가지가 있습니다. 내 문제를 해결하는 다른 옵션은 무엇입니까? 어떤 충고? –

+0

수동으로 내가이 시점에 도착하면, Enter 키를 눌러 창/팝업을 받아들이므로 내 스크립트에서 Enter 키를 눌러야하지만 내 경로가 점으로 바뀌기 때문에 작동하지 않습니다. " : S –

답변

0

사용 AutoIt을 예제 코드 : 당신은 전혀 대화 상자를 열지한다

import autoit 
from selenium import webdriver 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.keys import Keys 

ActionChains(driver).move_to_element(driver.find_element_by_xpath("//div[@class='upload_button']")).click().perform() 
handle = "[CLASS:#32770; TITLE:Open]" 
autoit.win_wait(handle, 60) 
autoit.control_set_text(handle, "Edit1", "\\file\\path") 
autoit.control_click(handle, "Button1")