如何通过特定的子/后代元素选择元素

时间:2018-11-26 04:51:35

标签: python selenium selenium-webdriver web-scraping

<a class="author " name="baut001" href="#!">
   <span class="content">
      <span class=" given-name">first name</span>
      <span class=" surname">Last name</span>
      <svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
         <path d="m22.6 77"></path>
      </svg>
   </span>
</a>
<a class="author" name="baut002" href="#!">
<span class="content">
<span class="text given-name">first name</span>
<span class="text surname">Last name</span>
<span class="author-ref" id="baff002"><sup>a</sup></span>
</span>
</a>
<a class="author" name="baut003" href="#!">
   <span class="content">
      <span class=" given-name">first name</span>
      <span class=" surname">Last name</span>
      <svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
         <path d="m22.6 77"></path>
      </svg>
   </span>
</a>

有三个<a>标签,其中两个包含<svg>标签,我想单击这些元素。

2 个答案:

答案 0 :(得分:1)

您可以在定位器中指定java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at com.example.RedisExampleApplication.main(RedisExampleApplication.java:23) [classes/:na] Caused by: org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1 at org.springframework.data.redis.repository.support.QueryByExampleRedisExecutor.findOne(QueryByExampleRedisExecutor.java:107) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_162] at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE] at com.sun.proxy.$Proxy57.findOne(Unknown Source) ~[na:na] at com.example.RedisExampleApplication.run(RedisExampleApplication.java:45) [classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] ... 5 common frames omitted 标签。

您可以使用<svg>

css_selector

driver.find_elements_by_css_selector('.author svg')

xpath

答案 1 :(得分:1)

如果要查找包含另一个节点的节点,则可能需要使用以下格式的XPath

  1. 通过子节点:

    //parent_node[child_node]
    

    //parent_node[child::child_node]
    
  2. 通过后代节点:

    //anscestor_node[.//descendant_node]
    

    //anscestor_node[descendant::descendant_node]
    

如果要选择包含a个后代节点的svg节点,可以尝试;

links_with_svg = driver.find_elements_by_xpath('//a[.//*[local-name()="svg"]]')

links_with_svg = driver.find_elements_by_xpath('//a[descendant::*[local-name()="svg"]]')