java 我无法使用 selenium webdriver 选择下拉菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17784066/
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
I am not able to select the dropdown using selenium webdriver please
提问by user2606274
I am trying to select the drop down of this site and proceed to buy a show, but I am not able to do so please help.
我正在尝试选择该网站的下拉菜单并继续购买节目,但我无法这样做,请帮助。
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.theatrepeople.com/");
driver.findElement(By.id("edit-show")).click();
new Select(driver.findElement(By.id("edit-show"))).selectByVisibleText("The 39 Steps");
driver.findElement(By.id("edit-date-datepicker-popup-0")).click();
driver.findElement(By.linkText("27")).click();
driver.findElement(By.id("edit-ticket-no")).click();
new Select(driver.findElement(By.id("edit-ticket-no"))).selectByVisibleText("1 ticket");
driver.findElement(By.id("edit-submit-1")).click();
回答by LaurentG
There is no reason to click on the selectform (driver.findElement(By.id("edit-show")).click()
), you just want to select an element (using the Select
class). This is also probably the reason why your code is not working. You should remove this line and it should work.
没有理由单击选择表单 ( driver.findElement(By.id("edit-show")).click()
),您只想选择一个元素(使用Select
类)。这也可能是您的代码不起作用的原因。您应该删除此行,它应该可以工作。
回答by Vinay
The following code will work WebDriver driver = new ChromeDriver();
以下代码将起作用 WebDriver driver = new ChromeDriver();
driver.get("http://www.theatrepeople.com/");
WebElement dropDown = driver.findElement(By.id("edit-ticket-no"));
Select sel = new Select(dropDown);
sel.selectByVisibleText("1 ticket");
回答by Vinay
Use the following code. It uses java script to select a text based upon it's valued. Really nice question. I also got to learn.
使用以下代码。它使用 java 脚本根据文本的值来选择文本。真的很好的问题。我也得学习了。
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "D:\ToCustomer_31_5_13\src\main\resources\Drivers\chromedriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.theatrepeople.com/");
driver.findElement(By.id("edit-show")).click();
WebElement show = driver.findElement(By.xpath("//div[@id = 'edit-show-wrapper']//div[@id = 'showNameWrap']"));
List<WebElement> l = show.findElements(By.tagName("option"));
String valueToSelect = getAttibuteValueForShow(l, "The American Plan");
driver.findElement(By.id("mini-basket-ajax")).click();
selectValueInDropDown(valueToSelect);
}
public static String getAttibuteValueForShow(List<WebElement> li, String showName)
{
int j =0;
String value = null;
for(int i =0; i<li.size(); i++)
{
j = j +1;
String dropDownText = li.get(i).getText();
if(dropDownText.equalsIgnoreCase(showName))
{
value = driver.findElement(By.xpath("//div[@id = 'edit-show-wrapper']//div[@id = 'showNameWrap']//option[" + j +"]")).getAttribute("value");
System.out.println(value);
break;
}
}
return value;
}
public static void selectValueInDropDown(String value)
{
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsCmd = "document.getElementsByName('show')[0].value='" + value + "'";
js.executeScript(jsCmd);
}