无法从硒的下拉菜单中选择选项

时间:2019-04-03 07:34:48

标签: java selenium selenium-webdriver

我是硒的新手,我发现很难从下拉菜单中获得选项,并且如果班级之间有空格,该如何获取className,请帮助我,谢谢。

我在selectbyindex中使用了select,同时也显示了部分可见的文本,但没有任何效果。

这是HTML代码

<div class="form-group" xpath="1">
  <label>Source of Info</label>      
 <!-- <select class="form-control flat-control-inner" ng-model="userPersonal.sourceOfInfo.id" required> 
  <option ng-value="0">--Select--</option>          
  <option data-ng-repeat="si in sourceOfInfoList" data-ng-value="{{si.id}}" ng-selected="si.id==userPersonal.sourceOfInfo.id">{{si.name}}</option>                                  
  </select> -->
     <select class="form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse" ng-model="userPersonal.sourceOfInfo.id" ng-options="si.id as si.name for si in sourceOfInfoList" style="">
      <option value="" class="">--Select--</option>
      <option label="Newspaper" value="number:1">Newspaper</option>
      <option label="Facebook" value="number:2" selected="selected">Facebook</option>
      <option label="Twitter" value="number:3">Twitter</option>
      <option label="Television" value="number:4">Television</option>
      <option label="Others" value="number:5">Others</option>
    </select>   
  </div>

这是我尝试过的

WebElement source_dropdown = driver.findElement(By.xpath("//select[@class='form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse']"));
Select source = new Select(source_dropdown);
source.selectByIndex(1);

这就是我在控制台中得到的

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@class='form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse']"}

3 个答案:

答案 0 :(得分:0)

使用2019-04-03 10:06:29.101 INFO 9439 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'primary' 2019-04-03 10:06:29.249 WARN 9439 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailController': Unsatisfied dependency expressed through field 'emailRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emailRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: methods with same signature getSessionFactory() but incompatible return types: [interface org.hibernate.ogm.OgmSessionFactory, interface org.hibernate.engine.spi.SessionFactoryImplementor] 2019-04-03 10:06:29.249 INFO 9439 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'primary' 2019-04-03 10:06:29.250 INFO 9439 --- [ main] o.h.o.d.m.impl.MongoDBDatastoreProvider : OGM001202: Closing connection to MongoDB 2019-04-03 10:06:29.252 INFO 9439 --- [ main] org.mongodb.driver.connection : Closed connection [connectionId{localValue:3, serverValue:24}] to localhost:27017 because the pool has been closed. 2019-04-03 10:06:29.257 INFO 9439 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2019-04-03 10:06:29.263 WARN 9439 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [cluster-ClusterId{value='5ca46983fa224724df0fc9b0', description='null'}-localhost:27017] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078) com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.waitForSignalOrTimeout(DefaultServerMonitor.java:229) com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.waitForNext(DefaultServerMonitor.java:210) com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:157) java.lang.Thread.run(Thread.java:748) 2019-04-03 10:06:29.264 WARN 9439 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [CleanCursors-1-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078) java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093) java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809) java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) java.lang.Thread.run(Thread.java:748) 2019-04-03 10:06:29.283 INFO 9439 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-04-03 10:06:29.301 ERROR 9439 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailController': Unsatisfied dependency expressed through field 'emailRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emailRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: methods with same signature getSessionFactory() but incompatible return types: [interface org.hibernate.ogm.OgmSessionFactory, interface org.hibernate.engine.spi.SessionFactoryImplementor] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE] at my.project.MyProjectApplication.main(MyProjectApplication.java:19) [classes/:na] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emailRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: methods with same signature getSessionFactory() but incompatible return types: [interface org.hibernate.ogm.OgmSessionFactory, interface org.hibernate.engine.spi.SessionFactoryImplementor] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] ... 19 common frames omitted Caused by: java.lang.IllegalArgumentException: methods with same signature getSessionFactory() but incompatible return types: [interface org.hibernate.ogm.OgmSessionFactory, interface org.hibernate.engine.spi.SessionFactoryImplementor] at sun.misc.ProxyGenerator.checkReturnTypes(ProxyGenerator.java:712) ~[na:1.8.0_201] at sun.misc.ProxyGenerator.generateClassFile(ProxyGenerator.java:461) ~[na:1.8.0_201] at sun.misc.ProxyGenerator.generateProxyClass(ProxyGenerator.java:339) ~[na:1.8.0_201] at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:639) ~[na:1.8.0_201] at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:557) ~[na:1.8.0_201] at java.lang.reflect.WeakCache$Factory.get(WeakCache.java:230) ~[na:1.8.0_201] at java.lang.reflect.WeakCache.get(WeakCache.java:127) ~[na:1.8.0_201] at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:419) ~[na:1.8.0_201] at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:719) ~[na:1.8.0_201] at org.springframework.orm.jpa.ExtendedEntityManagerCreator.createProxy(ExtendedEntityManagerCreator.java:233) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.orm.jpa.ExtendedEntityManagerCreator.createProxy(ExtendedEntityManagerCreator.java:200) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.orm.jpa.ExtendedEntityManagerCreator.createApplicationManagedEntityManager(ExtendedEntityManagerCreator.java:104) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:500) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:679) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE] at com.sun.proxy.$Proxy102.createEntityManager(Unknown Source) ~[na:na] at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:302) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE] at com.sun.proxy.$Proxy105.getDelegate(Unknown Source) ~[na:na] at org.springframework.data.jpa.provider.PersistenceProvider.fromEntityManager(PersistenceProvider.java:250) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.<init>(JpaRepositoryFactory.java:84) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.createRepositoryFactory(JpaRepositoryFactoryBean.java:106) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.doCreateRepositoryFactory(JpaRepositoryFactoryBean.java:98) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.data.repository.core.support.TransactionalRepositoryFactoryBeanSupport.createRepositoryFactory(TransactionalRepositoryFactoryBeanSupport.java:80) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:270) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:121) ~[spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1821) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] ... 29 common frames omitted 处理动态元素。如果代码不起作用,请检查是否有Interface org.hibernate.ogm.engine.spi.OgmSessionFactoryImplementor 可用,然后必须首先切换到WebDriverWait才能访问{{1 }}元素。

iframe

答案 1 :(得分:0)

您无法找到下拉列表本身,请尝试使用具有类似属性的xpath

//select[@ng-model='userPersonal.sourceOfInfo.id']

//div[@class='form-group']/following::select

答案 2 :(得分:0)

尝试使用此选项来选择显示的文本与参数匹配的选项

xpath = //select[@ng-model='userPersonal.sourceOfInfo.id']
WebElement dropDownListBox = driver.findElement(By.xpath("//select[@ng-model='userPersonal.sourceOfInfo.id']"));
Select clickThis = new Select(dropDownListBox);
clickThis.selectByVisibleText(value);//value is a option value