Java 如何通过selenium webdriver中的sendKeys传递变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33563054/
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
how to pass a variable through sendKeys in selenium webdriver?
提问by Rahul
I want to pass the float variable 'f' through sendKeys in the below program.Can someone please let me know the same? As of now, it is throwing
我想在下面的程序中通过 sendKeys 传递浮点变量“f”。有人可以让我知道吗?截至目前,它正在抛出
"The method sendKeys(CharSequence...) in the type WebElement is not applicable for the arguments ".
“WebElement 类型中的方法 sendKeys(CharSequence...) 不适用于参数”。
Code:
代码:
public static String isEditable(String s1) {
f=Float.parseFloat(s1);
System.out.println(f);
boolean bool=webDriver.findElement(By.xpath("expression")).isEnabled();
if(bool) {
if((f<0) || (f>6)) {
error="Value must be between 0.00% and 6.00%";
System.out.println(error);
} else {
webDriver.findElement(By.xpath(""expression")).sendKeys(f);
}
} else {
error="Please enter a valid Number";
}
return error;
}
采纳答案by JRodDynamite
Convert the float to a string:
将浮点数转换为字符串:
webDriver.findElement(By.xpath("...")).sendKeys(Float.toString(f));
回答by d0x
Can you try passing s1
instead of f
. Because the method takes a string, not a float.
你可以尝试通过s1
而不是f
. 因为该方法需要一个字符串,而不是一个浮点数。
Your method should look like this:
您的方法应如下所示:
String selector = "expression";
webDriver.findElement(By.xpath(selector)).sendKeys(f);
And please use better variable names like userInput
instead of s1
or userInputAsFloat
instead of f
or investmentInputVisible
instead of bool
etc.
并且请使用更好的变量名称,例如userInput
代替s1
或userInputAsFloat
代替f
或investmentInputVisible
代替bool
等。
回答by JeffC
I know you already accepted an answer but I wanted to clean up your code a little and give you some feedback.
我知道你已经接受了一个答案,但我想稍微清理一下你的代码并给你一些反馈。
I changed the name of the function because a function named
isEditable()
should return aboolean
indicating whether some field is editable. That's not what your function is doing so it should be given a more appropriate name. I made a guess at what the actual name should be... I could be way off but you should name it something more along the lines of what it's actually doing... putting text in a field.I removed the
isEnabled()
check because that should be done in the function that sets the fund number. Each function should do one thing and only one thing. This function validates that the rate passed is in a valid range and then puts it in the field.I removed the duplicate code that was scraping the INPUT twice. Just do it once, save it in a variable, and reuse that variable. In this case, there's no need to scrape it twice.
and as d0x said, you shouldn't convert the
s1
string to afloat
and then back tostring
when yousendKeys()
... just send thes1
string. Translating it back doesn't help readability, it just means you wrote more code that someone after you will need to understand. Favor clean code... it's always more readable.public static String enterRate(String s1) { f = Float.parseFloat(s1); WebElement input = webDriver.findElement(By.xpath(".//*[@id='p_InvestmentSelection_4113']/div/div/div[5]/div/ul/li/div[3]/div[2]/label/div[1]/input")); if ((f < 0) || (f > 6)) { error = "Value must be between 0.00% and 6.00%"; } else { input.sendKeys(s1); } return error; }
我更改了函数的名称,因为命名的函数
isEditable()
应该返回一个boolean
指示某个字段是否可编辑的值。这不是你的函数所做的,所以应该给它一个更合适的名字。我猜测了实际名称应该是什么......我可能会离题,但你应该更接近它实际所做的事情......将文本放在一个字段中。我删除了
isEnabled()
支票,因为这应该在设置基金编号的函数中完成。每个函数都应该做一件事,而且只做一件事。此函数验证传递的汇率是否在有效范围内,然后将其放入字段。我删除了两次抓取 INPUT 的重复代码。只需执行一次,将其保存在一个变量中,然后重用该变量。在这种情况下,不需要刮两次。
正如 d0x 所说,您不应该将
s1
字符串转换为 afloat
然后再返回到string
您sendKeys()
……只需发送s1
字符串即可。将其翻译回来无助于可读性,它只是意味着您编写了更多的代码,您之后的人需要理解这些代码。喜欢干净的代码......它总是更具可读性。public static String enterRate(String s1) { f = Float.parseFloat(s1); WebElement input = webDriver.findElement(By.xpath(".//*[@id='p_InvestmentSelection_4113']/div/div/div[5]/div/ul/li/div[3]/div[2]/label/div[1]/input")); if ((f < 0) || (f > 6)) { error = "Value must be between 0.00% and 6.00%"; } else { input.sendKeys(s1); } return error; }