How does dot(.) in xpath to take multiple form in identifying an element and matching a text

前端 未结 2 1011
夕颜
夕颜 2020-12-22 03:07

I have the below dom structure:

Associated Elements&a
2条回答
  •  不思量自难忘°
    2020-12-22 03:26

    As selenium is tagged so this answer would be based on xpath-1.0 and the associated XML Path Language (XPath) Version 1.0 specifications.


    contains(string, string)

    The function boolean contains(string, string) returns true if the first argument string contains the second argument string, and otherwise returns false. As an example:

    //h3[contains(.,'Associated Elements')]
    

    Text Nodes

    Character data is grouped into text nodes. As much character data as possible is grouped into each text node. The string-value of a text node is the character data. A text node always has at least one character of data. In the below example, text() selects all text node children of the context node:

    //h3[text()='Associated Elements']
    

    In your usecase, within the HTML the text Associated Elements  (5) have   which is alternatively referred to as a fixed space or hard space, NBSP (non-breaking space) used in programming to create a space in a line that cannot be broken by word wrap. Within HTML,   allows you to create multiple spaces that are visible on a web page and not only in the source code.


    Analyzing your code trials

    Your first code trial with:

    //h3[contains(.,'Associated Elements')]
    

    locates the element as it successfully identifies with partial text Associated Elements

    Your second code trial with:

    //h3[contains(text(),'Associated Elements')]
    

    fails as the element contains some more characters e.g.   in addition to the text Associated Elements.


    Reference

    You can find a couple of relevant discussions in:

    • How to locate the button element using Selenium through Python
    • What does contains(., 'some text') refers to within xpath used in Selenium
    • While fetching all links,Ignore logout link from the loop and continue navigation in selenium java

提交回复

热议问题