2012-12-25 54 views
1

任何人都可以檢查this html頁面。我無法點擊此處的「瀏覽」按鈕。 我試過如下: -無法點擊文件上傳在webdriver

//1st approach 
>driver.switchTo().frame(0) 
>driver.findElement(By.id("File1")).click 

//2nd Approach 
>(new Actions).moveToElement(..).sendKeys("Keys.ENTER") // with this i could 

//3rd by executing JS in webdriver 
>document.findElementById('').value = 'C://.. ' ; 

似乎沒有任何工作。任何人都可以請幫忙?

回答

1

試試這個

driver.switchTo().frame(driver.findElement(By.xpath("//*[contains(@src,'/convert-pdf/default.aspx')]"))); 
driver.findElement(By.id("File1")).sendKeys("C:\\testFile.xls"); 
0
  1. driver.findElement(By.id("File1")).click()在年底失蹤。
  2. 的頁面有可能會導致您不能夠訪問iframe錯誤:

不安全JavaScript嘗試與URL http://bcldoc2pdflb-1026005913.us-west-1.elb.amazonaws.com/convert-pdf/default.aspx 從幀幀訪問使用URL http://www.pdfonline.com/convert-pdf/default-b.aspx?utm_expid=127285-27&utm_referrer=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F14031004%2Funable-to-click-file-upload-in-webdriver。 域,協議和端口必須匹配。

我(在瀏覽器的開發工具通過JavaScript控制檯)手動試圖獲得元素,但無法得到的iframe:

f = document.evaluate("//*[contains(@src,'/convert-pdf/default.aspx')]", document, null, XPathResult.ANY_TYPE, null) 
XPathResult 
ifr = f.iterateNext() 
<iframe src=​"http:​/​/​bcldoc2pdflb-1026005913.us-west-1.elb.amazonaws.com/​convert-pdf/​default.aspx" width=​"460px" height=​"670px" frameborder=​"0">​…​</iframe>​ 
ifr.contentDocument 
Unsafe JavaScript attempt to access frame with URL http://bcldoc2pdflb-1026005913.us-west-1.elb.amazonaws.com/convert-pdf/default.aspx from frame with URL http://www.pdfonline.com/convert-pdf/default-b.aspx?utm_expid=127285-27&utm_referrer=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F14031004%2Funable-to-click-file-upload-in-webdriver. Domains, protocols and ports must match. 
null 
d = ifr.contentWindow.document 
Unsafe JavaScript attempt to access frame with URL http://bcldoc2pdflb-1026005913.us-west-1.elb.amazonaws.com/convert-pdf/default.aspx from frame with URL http://www.pdfonline.com/convert-pdf/default-b.aspx?utm_expid=127285-27&utm_referrer=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F14031004%2Funable-to-click-file-upload-in-webdriver. Domains, protocols and ports must match. 
+0

感謝Alex.But我可以訪問同樣的IFrame中的其他元素(輸出文本字段和電子郵件字段)。那怎麼可能呢? –

+0

@RohitP,你有沒有嘗試Santoshsarma建議的?我試過了,它對我有用(在Python中,但這應該不重要) –

0

框架沒有在IDE識別。可能的方法是用「AutoIT」來完成。我試過了,它解決了。下面是AutoIt的

Send("{ENTER}") Sleep(1000) Send("D:\questiontovetri.txt") Sleep(1000) Send("{TAB}") Sleep(1000) Send("{TAB}") Sleep(1000) Send("{ENTER}") Sleep(1000) Send("{TAB}") Sleep(1000) Send("{TAB}") Sleep(1000) Send("{TAB}") Sleep(1000) Send("{TAB}") Sleep(1000) Send("{ENTER}") Sleep(1000)

要使用的AutoIt,你需要下載示例代碼。請參閱「http://veera-myseleniumblog.blogspot.com/2011/11/handling-authentication-dialog-box.html」援助

一旦你轉換你AutoIt腳本可以調用Java代碼的EXE喜歡

driver.get("http://www.pdfonline.com/convert-pdf/"); driver.findElement(By.xpath("//a[contains(text(),'Upload a File to Convert...')]")).click(); Runtime run = Runtime.getRuntime(); Process pp=run.exec("D:\\blog.exe"); Thread.sleep(5000); driver.findElement(By.xpath("//a[contains(text(),'Download PDF file')]")).click();

這是工作,我已經測試過它

相關問題