Java 从下拉列表中选择元素
我想在Joomla中测试一个应用程序。 我有一个包含以下代码的下拉列表:Java 从下拉列表中选择元素,java,selenium,joomla,Java,Selenium,Joomla,我想在Joomla中测试一个应用程序。 我有一个包含以下代码的下拉列表: <div class="control-group"> <div class="control-label"> <label id="jform_category-lbl" for="jform_category" class="required"> Categoria<span class="star"> *
<div class="control-group">
<div class="control-label">
<label id="jform_category-lbl" for="jform_category" class="required">
Categoria<span class="star"> *</span>
</label>
</div>
<div class="controls">
<select id="jform_category" name="jform[category]" class="required" required aria-required="true">
<option value="9">stanza singola</option>
<option value="10">stanza doppia</option>
<option value="11">posto letto</option>
</select>
</div>
</div>
我正在使用Java来测试这个站点。
我如何从下拉列表中选择节doppia选项?您考虑过使用类吗
我已经在你提到的网站上试过了,它对我很有效。 实际上,您需要使用自定义xpath从下拉列表中选择值并将其存储在列表中。然后单击所需的值 有时选择不起作用,您可以使用此解决方法在下拉列表中选择值 下面是同样的工作代码
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SelectDropdown {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://bachecalloggi.listedisinistra.org/index.php/annunci");
driver.findElement(By.xpath("//button[contains(text(),'Ricerca Avanzata')]")).click();
select1(driver);
}
public static void select1(WebDriver driver) {
//Clicking on the Element to open the dropdown.
WebElement clickme = driver.findElement(By.xpath("//*[@id='filter_energy_class_chzn']/a/span"));
clickme.click();
//Sometime need to wait, as it take some time to load the values in dropdown.
//Thread.sleep(3000);
//Picking all the value from Dropdown. Use Custom Xpath for this.
List<WebElement> options = driver.findElements(By.xpath("//*[@id='filter_energy_class_chzn']//*[@class='chzn-results']/li"));
for (int i=0; i<options.size(); i++){
if (options.get(i).getText().equalsIgnoreCase("B")){
options.get(i).click();
}
}
}
}
创建Select对象会有帮助,这在第一个答案中提到。我尝试了,但没有成功:我在公共页面上遇到了相同的问题:这是代码:`public void testFilterAds抛出异常{driver.findElementBy.xpath//button[containstext,'Ricerca Avanzata'].click;WebElement elemnet=driver.findElementBy.idfilter\u energy\u class;Select=new Selectelemnet;//按值Select.selectByValueb;//按索引Select.selectByIndex2;//按文本Select.selectByVisibleTextB;Thread.sleep500000;}`未选择任何内容:您使用的代码与我使用的代码不同。您可以在第页的filter_energy_class下拉列表中发布一个completa class以选择B吗?请
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SelectDropdown {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://bachecalloggi.listedisinistra.org/index.php/annunci");
driver.findElement(By.xpath("//button[contains(text(),'Ricerca Avanzata')]")).click();
select1(driver);
}
public static void select1(WebDriver driver) {
//Clicking on the Element to open the dropdown.
WebElement clickme = driver.findElement(By.xpath("//*[@id='filter_energy_class_chzn']/a/span"));
clickme.click();
//Sometime need to wait, as it take some time to load the values in dropdown.
//Thread.sleep(3000);
//Picking all the value from Dropdown. Use Custom Xpath for this.
List<WebElement> options = driver.findElements(By.xpath("//*[@id='filter_energy_class_chzn']//*[@class='chzn-results']/li"));
for (int i=0; i<options.size(); i++){
if (options.get(i).getText().equalsIgnoreCase("B")){
options.get(i).click();
}
}
}
}