如何使用 selenium webdriver 在 SafariDriver / Safari 中上传文件?
How to upload a file in SafariDriver / Safari using selenium webdriver?
你好,你能帮我在 Safari 中上传文件吗,Mac OS。
我已经提供了一个站点,我们可以通过单击 "Choose File" 按钮上传以下类型的文件(JPG、PNG、GIF、DOC、XLS、PDF、ZIP、RAR、ZIPX)
package Default;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
public class Safari_demo {
public static void main(String[] args) throws InterruptedException{
WebDriver driver = new SafariDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(6000);
driver.get("http://www.files.com/");
driver.findElement(By.id("uploadFields")).click();
//can you please help me with code in this line
//this is the place were i need to enter the address of the
//file which is in my system
driver.close();
}
}
所以文件上传对话框实际上是一个本地系统对话框而不是网络对话框,因此您不能使用 Selenium 与之交互。在本地实现的方法是将路径发送到 filePath 文本框,saucelabs 描述得很好:
For those of you doing this locally, all you need to do is use the
sendKeys command to type the local path of the file in any file field.
This works like a charm in all drivers. When moving this test to a
remote server (such as, for example, our Selenium 2 Cloud), all you
need to do is use the setFileDetector method to let WebDriver know
that you’re uploading files from your local computer to a remote
server instead of just typing a path. Almost magically, the file will
be base64 encoded and sent transparently through the JSON Wire
Protocol for you before writing the fixed remote path. This is an
excellent solution, as it lets you switch your tests from a local to
remote Driver without having to worry about changing your tests’ code.
试试这个。
fd.findElement(By.xpath(".//*[@id='uploadFields']/input"))
.sendKeys(
"your image path");
SafariDriver 好像不支持文件上传:https://code.google.com/p/selenium/issues/detail?id=4220
你好,你能帮我在 Safari 中上传文件吗,Mac OS。
我已经提供了一个站点,我们可以通过单击 "Choose File" 按钮上传以下类型的文件(JPG、PNG、GIF、DOC、XLS、PDF、ZIP、RAR、ZIPX)
package Default;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
public class Safari_demo {
public static void main(String[] args) throws InterruptedException{
WebDriver driver = new SafariDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(6000);
driver.get("http://www.files.com/");
driver.findElement(By.id("uploadFields")).click();
//can you please help me with code in this line
//this is the place were i need to enter the address of the
//file which is in my system
driver.close();
}
}
所以文件上传对话框实际上是一个本地系统对话框而不是网络对话框,因此您不能使用 Selenium 与之交互。在本地实现的方法是将路径发送到 filePath 文本框,saucelabs 描述得很好:
For those of you doing this locally, all you need to do is use the sendKeys command to type the local path of the file in any file field. This works like a charm in all drivers. When moving this test to a remote server (such as, for example, our Selenium 2 Cloud), all you need to do is use the setFileDetector method to let WebDriver know that you’re uploading files from your local computer to a remote server instead of just typing a path. Almost magically, the file will be base64 encoded and sent transparently through the JSON Wire Protocol for you before writing the fixed remote path. This is an excellent solution, as it lets you switch your tests from a local to remote Driver without having to worry about changing your tests’ code.
试试这个。
fd.findElement(By.xpath(".//*[@id='uploadFields']/input"))
.sendKeys(
"your image path");
SafariDriver 好像不支持文件上传:https://code.google.com/p/selenium/issues/detail?id=4220