【问题标题】:How to identify element by text when text is spanned over multiple lines using Selenium使用 Selenium 跨越多行文本时如何通过文本识别元素
【发布时间】:2020-08-31 17:55:24
【问题描述】:

是否可以使用包含文本来编写 xpath,例如(以下是我想要的但不起作用)

//ul[@role='listbox']/..//span[contains(text(),'Fast-Food Restaurent')] 

页面代码:

<span class="item-title" md-highlight-text="searchText" md-highlight-flags="i">Fast-<span class="highlight">Food</span> Restaurant</span>

当我输入单词food时,它是一个自动完成的文本框,有一些选项,我想从中选择Fast-Food Restaurant。

提前致谢。

【问题讨论】:

    标签: selenium selenium-webdriver xpath webdriver xpath-1.0


    【解决方案1】:

    要识别元素,您可以使用以下基于Locator Strategies

    • 使用文本FoodFast

      //ul[@role='listbox']/..//span[.//span[text()='Food']][contains(.,'Fast-')]
      
    • 使用文本FoodRestaurant

      //ul[@role='listbox']/..//span[.//span[text()='Food']][contains(.,'Restaurant')]
      
    • 使用文本FoodFastRestaurant

      //ul[@role='listbox']/..//span[.//span[text()='Food']][contains(.,'Fast-') and contains(.,'Restaurant')]
      

    【讨论】:

      【解决方案2】:

      您可以根据其字符串表示选择所需的span节点:

      //span[.='Fast-Food Restaurant']
      

      【讨论】:

        【解决方案3】:

        您可以将路径修改为以下内容:

        //ul[@role='listbox']/..//span[contains(normalize-space(), 'Fast-Food Restaurant')] 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-30
          • 1970-01-01
          • 1970-01-01
          • 2011-08-18
          • 2017-10-27
          • 1970-01-01
          • 2019-05-16
          • 2011-01-05
          相关资源
          最近更新 更多