【问题标题】:Selenium using xpath - How to get elements text using their sibling element textSelenium 使用 xpath - 如何使用其兄弟元素文本获取元素文本
【发布时间】:2017-02-14 06:54:05
【问题描述】:

我卡在一端,无法获取文本值。

为了更好的查看:-

<div class="">
 <span class="address_city">Glenwood</span>
 <span class="address_state">GA</span>
 <span class="address_zip xh-highlight">30428</span>
</div>

我使用以下 xpath 识别类 address_zip...

//*[contains(text(),'30428')]

如何获取文本值GAGlenwood

【问题讨论】:

    标签: html selenium xpath selenium-webdriver gettext


    【解决方案1】:

    它不起作用,因为相同的类名有重复,这就是为什么我问有没有办法从 text() 指向上两个元素。

    你可以在下面使用xpath :-

    • 获取格伦伍德文本:

      .//div[span[text() = '30428']]/span[@class = 'address_city']
      
    • 获取GA文本:

      .//div[span[text() = '30428']]/span[@class = 'address_state']
      

    【讨论】:

      【解决方案2】:

      您可以使用preceding-sibling

      //*[contains(text(),'30428')]/preceding-sibling::span[@class='address_city']
      //*[contains(text(),'30428')]/preceding-sibling::span[@class='address_state']
      

      您还可以找到邮政编码元素并使用它

      WebElement zip = driver.findElement(By.xpath("//*[contains(text(),'30428')]"));
      String city = zip.findElement(By.xpath("//preceding-sibling::span[@class='address_city']")).getText();
      String state = zip.findElement(By.xpath("//preceding-sibling::span[@class='address_state']")).getText();
      

      【讨论】:

      • 谢谢,这正是我想要的。
      【解决方案3】:

      你为什么不直接使用:

      string city =Driver.FindElement(By.ClassName("address_city")).Text;
      string state =Driver.FindElement(By.ClassName("address_state")).Text;
      

      如果这些类与其他元素重复:

      //first get the zip as you are doing now.
      IWebElement zip=Driver.FindElement(By.Xpath("//*[contains(text(),'30428')]"));
      
      //now get the father element.
      IWebElement father=zip.FindElement(By.XPath(".."));
      
      //now get all the spans
      IList<IWebElement> allElements=father.FindElements(By.TagName("span"));
      
      //reach the elements you want.
      IWebElement city=allElements.ElementAt(0);
      IWebElement state=allElements.ElementAt(1);
      

      【讨论】:

      • 它不起作用,因为相同的类名有重复,这就是为什么我问有没有办法从 text() 指向上两个元素。
      【解决方案4】:

      //[contains(text(),'30428')]/preceding-sibling::span[contains(@class,'address_city')] //[contains(text(),'30428')]/preceding-sibling::span[contains(@class,'address_state')]

      【讨论】:

        【解决方案5】:

        试试下面提到的xpath

        获取GA的价值

        //span[text()= '30428']/..//preceding-sibling::span[text()= 'GA')]
        

        xpath 的解释:-text 方法与&lt;span&gt; 标记一起使用,并使用preceding-sibling keyword 与另一个&lt;span&gt; 标记一起使用。

        //span[text()= 'Glenwood']/following-sibling::span[text()= 'GA']
        

        xpath 的解释:-text 方法与&lt;span&gt; 标签一起使用,并使用following-sibling keyword 与另一个&lt;span&gt; 标签一起使用。

        //span[text()= 'Glenwood']/..//following-sibling::span[text()= 'GA']
        

        xpath 的解释:- 使用text 方法和&lt;span&gt; 标记并继续使用另一个&lt;span&gt; 标记使用following-sibling keyword

        获取格伦伍德的价值

        //span[text()= 'GA']/..//preceding-sibling::span[text()= 'Glenwood']
        

        xpath 的解释:-text 方法与&lt;span&gt; 标签一起使用,然后使用preceding-sibling keyword 与另一个&lt;span&gt; 标签一起使用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-26
          • 1970-01-01
          • 2015-08-05
          • 2020-09-27
          • 1970-01-01
          • 2020-03-31
          相关资源
          最近更新 更多