I have a dropdown box that I would like to select a value using WebDriverJS. I\'ve looked at the user guide below and could not find out how to do it
This is not actually clicking the option, but it does in fact select it.
I am using webdriverjs and want to select the option by index, so did:
driver.click('#my_select_box').click('#my_select_box option:nth-child(3)')
The following code defines the available selectors in WebDriverJS:
webdriver.Locator.Strategy = {
'className': webdriver.Locator.factory_('class name'),
'class name': webdriver.Locator.factory_('class name'),
'css': webdriver.Locator.factory_('css selector'),
'id': webdriver.Locator.factory_('id'),
'js': webdriver.Locator.factory_('js'),
'linkText': webdriver.Locator.factory_('link text'),
'link text': webdriver.Locator.factory_('link text'),
'name': webdriver.Locator.factory_('name'),
'partialLinkText': webdriver.Locator.factory_('partial link text'),
'partial link text': webdriver.Locator.factory_('partial link text'),
'tagName': webdriver.Locator.factory_('tag name'),
'tag name': webdriver.Locator.factory_('tag name'),
'xpath': webdriver.Locator.factory_('xpath')
};
goog.exportSymbol('By', webdriver.Locator.Strategy);
Source: https://code.google.com/p/selenium/source/browse/javascript/webdriver/locators.js
Find the select element and click on it to display the dropdown
driver.findElement(//div//select[@id="elementId"]).click();
Then select from the options and click on it. I think selecting by xpath will really work better here.
driver.findElement(By.xpath('//div//select[@id="elementId"]//option[optionIndex]')).click();
I was using the following with ES6:
let select = driver.findElement(By.css("select"));
let options = select.findElements(By.css("option"));
options.then(values => {
return Promise.all(values.map(el => el.getText())).then(optTexts => {
return values[optTexts.indexOf('Some option text')].click();
});
});
I get same problem, and above solutions not working in my typeScript case
But I still find a solution:
await driver.findElement(By.id("ELEMENT_ID")).sendKeys("SOME_VALUE");
Due to driver will return a promise when get a selector element
So add await to do the next things