1

任何人都可以检查这个html 页面。我无法在此处单击“浏览”按钮。我尝试了以下方法:-

//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://.. ' ;

似乎没有任何效果。有人可以帮忙吗?

4

3 回答 3

1

尝试这个

driver.switchTo().frame(driver.findElement(By.xpath("//*[contains(@src,'/convert-pdf/default.aspx')]")));
driver.findElement(By.id("File1")).sendKeys("C:\\testFile.xls");
于 2012-12-26T07:35:30.450 回答
0
  1. Your driver.findElement(By.id("File1")).click has () missing at the end.
  2. The page has errors that might result in you not being able to access the iframe:

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.

I've tried manually (through JavaScript Console in browser's dev tools) to get the element, but couldn't get to the 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">​…​&lt;/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.
于 2012-12-26T16:36:16.843 回答
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();

这是有效的,我已经测试过了

于 2013-03-15T12:47:49.287 回答