最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How To select a Value From Drop-Down using Selenium? - Stack Overflow

programmeradmin3浏览0评论

Given Below is a Piece of Code which denotes a Drop-Down. I need to Select Date value in this Drop-down denoted By <option value="1" label="Date">Date</option>

<select id="type" class="text-input ng-pristine ng-valid ng-scope ng-touched" ng-style="cssStyle" name="type" ng-if="!options.hidePlaceholder" ng-model="result.type" qmx-observe-value="text" ng-disabled="options.readonly" ng-options="obj.value as obj.text group by obj.groupby for obj in selectData" style="font-size: 13px; opacity: 1; width: 100%;">
    <option class="ng-binding" value="">----</option>
    <option value="0" selected="selected" label="Text">Text</option>
    <option value="1" label="Date">Date</option>
    <option value="2" label="Numeric">Numeric</option>
    <option value="3" label="Switch">Switch</option>
    <option value="4" label="Map Location Marker">Map Location Marker</option>
    <option value="5" label="Picker">Picker</option>
    <option value="6" label="List">List</option>
    </select>

Following Methods didn't work.
1.) selecting this value using Select by importing org.openqa.selenium.support.ui.Select

Select elm = new Select(driver.findElement(By.xpath(".//*[@id='type']/option[3]")));
  elm.selectByVisibleText("Date");

Console shows:

Element should have been "select" but was "option"


2.) Clicking on the Drop-Down first to display option to be selected and then clicking on the option.

driver.findElement(By.xpath(".//*[@id='type']")).click();
driver.findElement(By.xpath(".//*[@id='type']/option[3]")).click();

Console shows:

DEBUG Element is missing an accessible name: id: type, tagName: SELECT, className: text-input ng-pristine ng-untouched ng-valid ng-scope


3.) Using JavascriptExecutor to get the click.

driver.findElement(By.xpath(".//*[@id='type']")).click();    
((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath(".//*[@id='type']/option[3]")));

Console shows:

DEBUG Element is missing an accessible name: id: type, tagName: SELECT, className: text-input ng-pristine ng-untouched ng-valid ng-scope


4.) Using Mouse-Over on Option to be selected in Drop-down and then performing click on it.

driver.findElement(By.xpath(".//*[@id='type']")).click();    
WebElement subdrop = driver.findElement(By.xpath(".//*[@id='type']/option[3]"));
        Actions action = new Actions(drive);
        action.moveToElement(subdrop).perform();
        Custom.Twait();
        action.click(subdrop).perform();

Console shows:

Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: POST /session/a37a745a-e40c-45a9-9760-8e01b451a017/moveto did not match a known mand (WARNING: The server did not provide any stacktrace information)

I have also added Wait in Between where i'm using this code. Here for simplicity i did not include it.

Need Help.

Given Below is a Piece of Code which denotes a Drop-Down. I need to Select Date value in this Drop-down denoted By <option value="1" label="Date">Date</option>

<select id="type" class="text-input ng-pristine ng-valid ng-scope ng-touched" ng-style="cssStyle" name="type" ng-if="!options.hidePlaceholder" ng-model="result.type" qmx-observe-value="text" ng-disabled="options.readonly" ng-options="obj.value as obj.text group by obj.groupby for obj in selectData" style="font-size: 13px; opacity: 1; width: 100%;">
    <option class="ng-binding" value="">----</option>
    <option value="0" selected="selected" label="Text">Text</option>
    <option value="1" label="Date">Date</option>
    <option value="2" label="Numeric">Numeric</option>
    <option value="3" label="Switch">Switch</option>
    <option value="4" label="Map Location Marker">Map Location Marker</option>
    <option value="5" label="Picker">Picker</option>
    <option value="6" label="List">List</option>
    </select>

Following Methods didn't work.
1.) selecting this value using Select by importing org.openqa.selenium.support.ui.Select

Select elm = new Select(driver.findElement(By.xpath(".//*[@id='type']/option[3]")));
  elm.selectByVisibleText("Date");

Console shows:

Element should have been "select" but was "option"


2.) Clicking on the Drop-Down first to display option to be selected and then clicking on the option.

driver.findElement(By.xpath(".//*[@id='type']")).click();
driver.findElement(By.xpath(".//*[@id='type']/option[3]")).click();

Console shows:

DEBUG Element is missing an accessible name: id: type, tagName: SELECT, className: text-input ng-pristine ng-untouched ng-valid ng-scope


3.) Using JavascriptExecutor to get the click.

driver.findElement(By.xpath(".//*[@id='type']")).click();    
((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath(".//*[@id='type']/option[3]")));

Console shows:

DEBUG Element is missing an accessible name: id: type, tagName: SELECT, className: text-input ng-pristine ng-untouched ng-valid ng-scope


4.) Using Mouse-Over on Option to be selected in Drop-down and then performing click on it.

driver.findElement(By.xpath(".//*[@id='type']")).click();    
WebElement subdrop = driver.findElement(By.xpath(".//*[@id='type']/option[3]"));
        Actions action = new Actions(drive);
        action.moveToElement(subdrop).perform();
        Custom.Twait();
        action.click(subdrop).perform();

Console shows:

Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: POST /session/a37a745a-e40c-45a9-9760-8e01b451a017/moveto did not match a known mand (WARNING: The server did not provide any stacktrace information)

I have also added Wait in Between where i'm using this code. Here for simplicity i did not include it.

Need Help.

Share Improve this question edited Jul 7, 2016 at 8:15 Saurabh Gaur 23.8k10 gold badges56 silver badges74 bronze badges asked Jul 7, 2016 at 6:59 Pawan JuyalPawan Juyal 2611 gold badge5 silver badges14 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

In your first option selenium clearly saying Element should have been "select" but was "option", means here you are providing the xpath for option while expecting only xpath for select.

Don't need to use other option as you provided, Just use your first option as below :-

Select elm = new Select(driver.findElement(By.id("type")));
elm.selectByVisibleText("Date");

or ByIndex

elm.selectByIndex(2);

or ByValue

elm.selectByValue("1");

If your first option unfortunatly not work I prefer you to use your third option Using JavascriptExecutor as below :-

WebElement select = driver.findElement(By.id("type"));

((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, "Date");

Hope it will help you...:)

In case you anyone looking solution in Selenium - Python : with JS call.

driver.execute_script("return document.getElementById('select id here').selectedIndex = '2'")

The select class as used in other languages was not available in typescript. Click seems to work when used with xPath.

import { Builder, By, Key, Locator, TargetLocator, until } from 'selenium-webdriver';

async function example() {
  const driver = new Builder().forBrowser('chrome').build();
  await driver.get('http://localhost:5500/website/index.html');
  const xPath = '//*[@id="cars"]/option[3]';
  const element = driver.findElement(By.xpath(xPath));
  await element.click();
}

Relevant HTML:

<label for="cars">Choose a car:</label>
<select name="cars" id="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

Basically our selector should target the option element and trigger Click event.

发布评论

评论列表(0)

  1. 暂无评论