51Testing软件测试论坛

标题: selenium 下拉框无法定位的问题 【很急】 [打印本页]

作者: hellen_jia    时间: 2015-12-28 17:53
标题: selenium 下拉框无法定位的问题 【很急】
大家好,我是selenium的新手,现在遇到一个关于如何定位下拉菜单的问题。

具体html 如下:
<select class="form-control ng-pristine ng-untouched ng-valid" ng-model="gus.curr_gu">
    <option value="? object:null ?"></option>
    <option class="ng-binding ng-scope" ng-repeat="gu in gus.gus | orderBy:'guname'" value="Shanghai">Shanghai</option>
    <option class="ng-binding ng-scope" ng-repeat="gu in gus.gus | orderBy:'guname'" value="Beijing">Beijing</option>
</select>

我用了CssSelector的定位方法, 麻烦大家帮忙看看这个有什么问题啊,怎么样可以正常定位到这个元素呢,非常感谢!
SelectElement SelectGU = new SelectElement(driver.FindElement(By.CssSelector("select.form-control.ng-pristine.ng-untouched.ng-valid")));
SelectGU.SelectByValue("Shanghai");

一直报错,下面是exception detail:
OpenQA.Selenium.NoSuchElementException was unhandled
  HResult=-2146233088
  Message=Unable to locate element: {"method":"css selector","selector":"select.form-control.ng-pristine.ng-untouched.ng-valid"}
  Source=WebDriver
  StackTrace:
       at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
       at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
       at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
       at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByCssSelector(String cssSelector)
       at OpenQA.Selenium.By.<>c__DisplayClass1e.<CssSelector>b__1c(ISearchContext context)
       at OpenQA.Selenium.By.FindElement(ISearchContext context)
       at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
       at DownloadNoGoList.Main(String[] args) in c:\CIO Project\Specflowstudy\1225\selenium\DownloadNoGoList\DownloadNoGoList\Program.cs:line 57
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:




        



作者: hellen_jia    时间: 2015-12-29 09:27
没有人知道吗?
作者: joykao    时间: 2015-12-29 14:24
你这个css的value写的对么?select.form-control.ng-pristine.ng-untouched.ng-valid,用这个吧SelectElement SelectGU = new SelectElement(driver.FindElement(By.CssSelector("select[class='form-control ng-pristine ng-untouched ng-valid']")));
作者: chop123    时间: 2016-2-18 18:33
你先要写点击这个下拉菜单的操作,已经操作了点开菜单后,然后写以下:
        List<WebElement> allOptions = driver.findElements(By.xpath("//select[@class=form-control ng-pristine ng-untouched ng-valid"));
        String targetText="XX";//这里是你下拉选项的text值
        for (int i = 0;i<allOptions.size();i++) {
            if(allOptions.get(i).getText().equals(targetText)){
                allOptions.get(i).click();
            }
        }




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2