Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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 方法选择selenium中的下拉值_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 方法选择selenium中的下拉值

Java 方法选择selenium中的下拉值,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我需要一个简单的方法将值发送到“dropID” 价值观是 阿联酋,巴林,阿曼。。。等 下拉式用户界面 我的xpath(全局) HTML <nav class="header__countries-menu--desktop mi-js-countries-menu-desktop" style="display: block;"> <ul> <li class="header__count

我需要一个简单的方法将值发送到“dropID

价值观是

阿联酋,巴林,阿曼。。。等

下拉式用户界面

我的xpath(全局)

HTML

<nav class="header__countries-menu--desktop mi-js-countries-menu-desktop" style="display: block;">
                  <ul>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=ae&amp;url=/Sponsored">UAE</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=bh&amp;url=/Sponsored">Bahrain</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=om&amp;url=/Sponsored">Oman</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=qa&amp;url=/Sponsored">Qatar</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=kw&amp;url=/Sponsored">Kuwait</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=eg&amp;url=/Sponsored">Egypt</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=jo&amp;url=/Sponsored">Jordan</a>

我对Selenium非常陌生,正在寻求帮助(1)不要使用绝对路径,这是一种不好的做法。请参见以下一些动态xpath示例:

假设您的CountryPage类中有此项

WebElement countryEle=driver.findelelement(By.xpath(“/[@class='header\uuu countries-menu--desktop mi js countries menu desktop']/[contains(text(),“+dynamicText+”)]”)

WebElement countryEle=driver.findelelement(By.xpath(“/[@class='header\uuuu country-selector--desktop\uuuu country']]/[contains(text(),“+dynamicText+”)]”)

(2) 您需要单击下拉列表以单击任何国家/地区:

driver.findElement(By.xpath("dropDown of your country ")).click();
(3) 将动态DropID发送到所选国家/地区:

public static void selectFromDropdown(WebDriver driver, WebElement element)
{
    driver.findElement(element).click();
}
(4) 调用您的方法:

selectFromDropdown(driver,CountryPage(driver,"UAE"))

下面是我尝试过的代码

    public void portfolioRenewalSearch(String portfolioId) throws Exception {

             By xpath2 = By.xpath("//*[@id='wrapper']/div[1]/header/div[2]/div[2]/div[1]/div[1]/div[2]/nav/ul/li[" + portfolioId + "]/a");

           //MY ACTION IS HERE

            return;
}

然后,我从我的测试用例中调用了

请添加要执行单击操作的下拉列表的html请检查更新的问题
    public void portfolioRenewalSearch(String portfolioId) throws Exception {

             By xpath2 = By.xpath("//*[@id='wrapper']/div[1]/header/div[2]/div[2]/div[1]/div[1]/div[2]/nav/ul/li[" + portfolioId + "]/a");

           //MY ACTION IS HERE

            return;
}