java Selenium WebDriver 中的 sendKeys()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13062453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:22:19  来源:igfitidea点击:

sendKeys() in Selenium WebDriver

javaseleniumselenium-webdriver

提问by cxyz

I am new to Selenium and I am trying to upload a file using WebDriver. Here I am trying to click browse button using dom element as follows:

我是 Selenium 的新手,我正在尝试使用 WebDriver 上传文件。在这里,我尝试使用 dom 元素单击浏览按钮,如下所示:

selenium.type("document.forms['UploadForm'].elements['browsebutton']",file.getAbsolutePath());

But since the approach did not work am trying to hit browse button using WebDriver element as below: How can i change my dom element to xpath or css selector as below?

但是由于该方法不起作用,我尝试使用 WebDriver 元素点击浏览按钮,如下所示: 如何将我的 dom 元素更改为 xpath 或 css 选择器,如下所示?

driver.findElement(By.cssSelector("input[type=\"file\"]")).click();

I cant write xpath as

我不能将 xpath 写为

selenium.click("xpath="//input[@name='uplaod' and @value='browsebutton']");

since i have multiple browse buttons with same name and value.. So i need to pick using dom element itself. How do i do it?

因为我有多个具有相同名称和值的浏览按钮..所以我需要选择使用 dom 元素本身。我该怎么做?

Thanks in advance for help.

预先感谢您的帮助。

Dominik i have tried using the below xpath since there is no name attribute:But not working

Dominik 我已经尝试使用下面的 xpath,因为没有 name 属性:但不工作

String upload="(//input[@name='bulkUnBlockUploadForm' and @value='requestFile'])[2]";
String button="(//input[@name='bulkUnBlockUploadForm' and @value='process'])[2]";

I tried using id as well:Not working

我也尝试使用 id:不工作

   String upload="(//input[@id='content' and @value='requestFile'])[1]";
    String button="(//input[@id='content' and @value='process'])[1]";

The issue is in my jsp,i have 2 browse buttons s with same id and same value,but different form.I have 2 submit buttons for each of browse buttons with same id and same value,but different forms.So when am using the above approaches its hitting both the submit buttons

问题出在我的 jsp 中,我有 2 个具有相同 id 和相同值但不同形式的浏览按钮。对于每个具有相同 id 和相同值但不同形式的浏览按钮,我有 2 个提交按钮。所以当我使用上面接近它同时点击提交按钮

回答by Ankit jain

This can upload a file, it is work for me.

这可以上传文件,这对我有用。

public static void main(String[] args) 
{
     WebDriver  driver = new FirefoxDriver();
     driver.get("http://www.freepdfconvert.com/");
     driver.findElement(By.id("UploadedFile")).sendKeys("C:\Users\Reputa\Downloads\HP1.pdf");        
     try {
            Thread.sleep(4000);
        } catch (Exception e) {}
     driver.findElement(By.name("pdfsubmit")).click();
        }

回答by sameer joshi

hi when iam beginner i also find a same issue someone told me you cant handle windows controls so use third party application such autoit , iam using autoit.

嗨,当我是初学者时,我也发现了同样的问题,有人告诉我您无法处理 Windows 控件,因此请使用第三方应用程序,例如 autoit,我正在使用 autoit。

    1. download autoit.
    2. no need of any jars just add Runtime,getruntime().execute('path of exe');in your code
    3.code of file upload is below

Local $hWnd=WinWait("[CLASS:#32770]","",10)
ControlFocus($hWnd,"","Edit1")
Sleep(2000)
ControlSetText($hWnd, "", "Edit1", "path of file to upload")
Sleep(2000)
ControlClick($hWnd, "","Button1");

4 run your java application still if you find query ask me.

4 如果您发现查询,请继续运行您的 Java 应用程序。

回答by dokaspar

If you have two buttons with equal attributes, then either rename them to be better accessible (for example by giving them a unique id) or try to change your XPath statement to something like this:

如果您有两个具有相同属性的按钮,那么要么将它们重命名以使其更易于访问(例如通过给它们一个唯一的 id)或尝试将您的 XPath 语句更改为如下所示:

String uploadButton1 = "(//input[@name='upload' and @value='browsebutton'])[1]";
String uploadButton2 = "(//input[@name='upload' and @value='browsebutton'])[2]";
driver.findElement(By.xpath(uploadButton1)).click();
driver.findElement(By.xpath(uploadButton2)).click();