Selenium RC“attach_file”仅支持* Firefox吗?有什么方法可以在谷歌浏览器中上传文件?任何建议或解决方法都非常感谢。Selenium:在Google Chrome中上传文件
回答
上传文件通常是一个POST请求,所以你实际上可以在不使用Selenium的情况下上传文件;除非你的网站需要Cookie,然后你需要webdriver.get_cookies()首先重建饼干
一个简单的例子:
# required package:
# http://pypi.python.org/pypi/MultipartPostHandler/0.1.0
import MultipartPostHandler, urllib2, cookielib
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
MultipartPostHandler.MultipartPostHandler)
path_to_file = r"abc.zip"
open_file = open(path_to_file,"rb")
param = { "file": open_file }
req = opener.open("http://www.yoursite.com/uploadfile", param)
open_file.close()
如果您使用的webdriver然后上传文件,你需要的是用“ sendKeys“键入文件路径。您需要“跳过”点击浏览按钮的部分,以打开一个对话框来选择文件。这对我工作的Java版本看起来像下面,
WebElement inputFilePath = driver.findElement(By.id("filepath"));
inputFilePath.sendKeys("/absolute/path/to/my/local/file");
也许还值得注意的是,使用sendKeys上传文件只能用于 – Ardesco 2013-03-18 09:02:05
使用IJavaScriptExecutor是改变上传输入栏点击能够使驾驶员铬不会弹出错误说这个元素是不可点击。
[SetUp]
public void SetupTest()
{
driver = new ChromeDriver();
baseURL = "";
verificationErrors = new StringBuilder();
}
[Test]
public void Test()
{
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
IWebElement element = driver.FindElement(By.Id("UploadFile_ButtonID"));
js.ExecuteScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", element);
Thread.Sleep(1000);
element.SendKeys("D:\\path\\test\\image.jpg");
}
没有上下文的代码是毫无意义的,你没有解释为什么你使用JavaScript Executor来设置元素可见,恕我直言,这可能是不好的做法。阻止您与隐藏元素进行交互,因为这是最终用户无法做到的事情,而不是盲目使用J avaScript强制元素可见,您应该实际上做最终用户会做什么来使元素可见。 – Ardesco 2013-03-18 09:00:05
您还使用Thread.sleep这又是一个不好的做法,要做的正确的事情是使用WebDriverWait对象和ExpectedConditions类来等待元素变为可见。这是一个粗劣的例子,没有解释和错误的代码练习-1 – Ardesco 2013-03-18 09:00:36
- 1. selenium webdriver .net chrome上传文件
- 2. Ajax上传Google Chrome的文件错误?
- 3. 在Google Chrome/Chromium和Safari中拖放文件上传?
- 4. 在C#中使用Selenium上传文件?
- 5. Selenium RC上传文件
- 6. selenium webdriver上传文件
- 7. cloneNode不克隆Google Chrome中文件上传元素的值
- 8. 无法上传selenium webdriver中的文件
- 9. 在Google App Engine中上传文件
- 10. JERSEY在Google App Engine中上传文件
- 11. Selenium上的图片/文件上传
- 12. Blueimp文件上传器 - Chrome文件夹上传限制
- 13. 上传文件到Google Drive
- 14. 如何使用Junit Selenium上传文件?
- 15. 用ubuntu上传selenium和python文件
- 16. selenium webdriver:通过拖放上传文件
- 17. 用selenium ide上传文件到Dropzone.js
- 18. Selenium Grid和Jenkins,上传文件
- 19. python selenium点击文件上传选项
- 20. 如何在Selenium上使用Google Chrome扩展程序?
- 21. 如何在Selenium中使用Windows上传对话框处理文件上传
- 22. 上传文件
- 23. 在Google Apps脚本上创建上传文件的文件夹
- 24. 如何在Selenium中通过AutoIt脚本动态上传文件?
- 25. 使用sendKeys()在Selenium中上传文件不起作用
- 26. 如何使用selenium webdriver在SafariDriver/Safari中上传文件?
- 27. 通过Selenium在AngularJS中自动上传文件
- 28. 如何在selenium webdriver中使用相对路径上传文件
- 29. 如何从Chrome扩展上传文件?
- 30. Chrome和Firefox文件上传浏览bug
[我张贴使用python在这里的答案。] [1] [1]:http://stackoverflow.com/a/11872608/471376 – JamesThomasMoon1979 2012-08-08 20:43:42