Selenium Select2 command for drop-down box

2019-06-13 16:04发布

问题:

am new to selenium and in my previous question Selenium IDE command for input type hidden it is using select2 please help me with the command to selection option for drop-down list.

i tried looking in here https://gist.github.com/3683275 but it doesn't seem to work for me

mouseDown('//a[@class="select2-choice select2-default"][1]')
mouseUp('//li[contains(@class,"select2-result")][1]')

回答1:

These commands open the options list, wait for it to appear, and finally choose the option labelled "California". Select2 can be customized in different ways, hope these commands work for you.

mouseDown      css=.select2-choice > div > b
waitForVisible css=.select2-results
mouseUp        css=.select2-result-label:contains('California') 


回答2:

WebElement element = driver.findElements(By.xpath(/*xpath*/));

Select select = new Select(element);

select.selectByVisibleText(value);


回答3:

Alternatively, it can also be handled by using clickAt()

clickAt('//a[@class="select2-choice select2-default"][1]');
waitForVisible("css=.select2-results");
clickAt('//li[contains(@class,"select2-result")][1]');