Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SeleniumWebDriver中选择用户将在其中传递参数的按钮?_Java_Selenium_Webdriver - Fatal编程技术网

Java 如何在SeleniumWebDriver中选择用户将在其中传递参数的按钮?

Java 如何在SeleniumWebDriver中选择用户将在其中传递参数的按钮?,java,selenium,webdriver,Java,Selenium,Webdriver,这是我的java代码 package com.ej.zob.modules; import java.awt.List; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import

这是我的java代码

package com.ej.zob.modules;

import java.awt.List;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class SetExchange {
public void Execute(String CountryName, String value)
{
    LaunchApplication.driver.findElement(By.linkText("SET")).click();
    LaunchApplication.driver.findElement(By.linkText("EXCHANGE RATE")).click();
    //LaunchApplication.driver.findElement(By.xpath("//div[.=\""+CountryName+"\"]/following-sibling::*")).findElement(By.xpath("//input[@maxlength='4']")).sendKeys(value);
    //LaunchApplication.driver.findElement(By.xpath(String.format("//input[@id=%s]",CountryName))).sendKeys(value);
    LaunchApplication.driver.findElement(By.xpath("//input[contains(@id,'new_"+CountryName+"_')]")).sendKeys(value);
    //LaunchApplication.driver.findElement(By.xpath("//input[contains(@onclick,'Albania')]")).click();
    LaunchApplication.driver.findElement(By.xpath("//input[contains(@onclick,'setExchangeRate('Afghanistan','AFN')')]")).click();
    WebElement w = LaunchApplication.driver.findElement(By.id("msg_"+CountryName+"_ALL"));
    LaunchApplication.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    System.out.println(w.getText());
    //LaunchApplication.driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    org.junit.Assert.assertEquals("Exchange Rate Saved Successfully",w.getText());
}
}
这是我的HTML

<div style="display: table-cell;width:270px" name="cell">
<input id="new_Afghanistan_AFN" type="text" maxlength="4">
<input type="button" value="SET" onclick="setExchangeRate('Afghanistan','AFN')" name="save">
让我们看第7行 LaunchApplication.driver.findElementBy.xpath//input[contains@onclick,'setExchangeRate'Afghanistan','AFN]。单击


在这一行中,我通过元素的属性和值来查找元素,但我想使用字符串CountryNameExecute方法参数,而不是值setExchangeRate'Afghanistan','AFN]。这意味着用户将从“我的主要方法”传递国家名称,并应单击该名称。假设用户从“主要方法”传递阿富汗,则应选择“阿富汗”按钮。希望您理解。

您可以使用如下方法生成定位器:

public string GetButtonCountryLocator(string countryName, string countryCode)
        {
            string buttonLocator = string.Format("//input[contains(@onclick,'setExchangeRate('{0}','{1}')')]", countryName, countryCode);
            return buttonLocator;
        }
代码在Java中是相同的

编辑: Java版本:

public final String GetButtonCountryLocator(String countryName, String countryCode)
{
            String buttonLocator = String.format("//input[contains(@onclick,'setExchangeRate('%1$s','%2$s')')]", countryName, countryCode);
            return buttonLocator;
}

正如您所看到的,findBy参数以字符串作为输入,所以您只需要将字符串与参数以正确的形式组合在一起,那么如何做到这一点呢?是否有任何xpath表达式可以执行此操作。