51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2642|回复: 3
打印 上一主题 下一主题

selenium 下拉框无法定位的问题 【很急】

[复制链接]
  • TA的每日心情
    奋斗
    2015-12-28 18:27
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2015-12-28 17:53:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    大家好,我是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:




            


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2015-12-28 18:27
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    2#
     楼主| 发表于 2015-12-29 09:27:07 | 只看该作者
    没有人知道吗?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2020-12-8 11:20
  • 签到天数: 605 天

    连续签到: 1 天

    [LV.9]测试副司令

    3#
    发表于 2015-12-29 14:24:34 | 只看该作者
    你这个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']")));

    评分

    参与人数 1测试积点 +10 收起 理由
    lsekfe + 10 恭喜您获得测试积点10

    查看全部评分

    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
    发表于 2016-2-18 18:33:28 | 只看该作者
    你先要写点击这个下拉菜单的操作,已经操作了点开菜单后,然后写以下:
            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();
                }
            }
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-26 16:57 , Processed in 0.071232 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表