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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:46:49  来源:igfitidea点击:

how to pass a variable through sendKeys in selenium webdriver?

javaseleniumselenium-webdriversendkeys

提问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 s1instead 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 userInputinstead of s1or userInputAsFloatinstead of for investmentInputVisibleinstead of booletc.

并且请使用更好的变量名称,例如userInput代替s1userInputAsFloat代替finvestmentInputVisible代替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.

我知道你已经接受了一个答案,但我想稍微清理一下你的代码并给你一些反馈。

  1. I changed the name of the function because a function named isEditable()should return a booleanindicating 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.

  2. 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.

  3. 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.

  4. and as d0x said, you shouldn't convert the s1string to a floatand then back to stringwhen you sendKeys()... just send the s1string. 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;
    }
    
  1. 我更改了函数的名称,因为命名的函数isEditable()应该返回一个boolean指示某个字段是否可编辑的值。这不是你的函数所做的,所以应该给它一个更合适的名字。我猜测了实际名称应该是什么......我可能会离题,但你应该更接近它实际所做的事情......将文本放在一个字段中。

  2. 我删除了isEnabled()支票,因为这应该在设置基金编号的函数中完成。每个函数都应该做一件事,而且只做一件事。此函数验证传递的汇率是否在有效范围内,然后将其放入字段。

  3. 我删除了两次抓取 INPUT 的重复代码。只需执行一次,将其保存在一个变量中,然后重用该变量。在这种情况下,不需要刮两次。

  4. 正如 d0x 所说,您不应该将s1字符串转换为 afloat然后再返回到stringsendKeys()……只需发送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;
    }