Skip to main content
Replace automatic Python syntax highlighting with HTML one
Source Link
Benjamin Loison
  • 5.8k
  • 4
  • 20
  • 37

In the HTML which you have provided:

<div>My Button</div> 
<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

In case the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 
<div> My Button</div> 

or at the end:

<div>My Button </div> 
<div>My Button </div> 

or at both the ends:

<div> My Button </div> 
<div> My Button </div> 

In these cases you have two options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

     my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

     driver.find_element_by_xpath("//div[normalize-space()='My Button']") 

XPath expression for variable text

In case the text is a variable, you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 

In the HTML which you have provided:

<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

In case the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 

or at the end:

<div>My Button </div> 

or at both the ends:

<div> My Button </div> 

In these cases you have two options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

     my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

     driver.find_element_by_xpath("//div[normalize-space()='My Button']") 

XPath expression for variable text

In case the text is a variable, you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 

In the HTML which you have provided:

<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

In case the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 

or at the end:

<div>My Button </div> 

or at both the ends:

<div> My Button </div> 

In these cases you have two options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

    my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

    driver.find_element_by_xpath("//div[normalize-space()='My Button']") 

XPath expression for variable text

In case the text is a variable, you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 
Updated answer
Source Link
undetected Selenium
  • 194.5k
  • 44
  • 304
  • 387

In the HTML which you have provided:

<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

In case the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 

or at the end:

<div>My Button </div> 

or at both the ends:

<div> My Button </div> 

In these cases you have two options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

     my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

     driver.find_element_by_xpath("//div[normalize-space()='My Button']]"Button']") 

XPath expression for variable text

In case the text is a variable, you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 

In the HTML which you have provided:

<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

In case the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 

or at the end:

<div>My Button </div> 

or at both the ends:

<div> My Button </div> 

In these cases you have two options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

     my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

     driver.find_element_by_xpath("//div[normalize-space()='My Button']]") 

XPath expression for variable text

In case the text is a variable, you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 

In the HTML which you have provided:

<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

In case the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 

or at the end:

<div>My Button </div> 

or at both the ends:

<div> My Button </div> 

In these cases you have two options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

     my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

     driver.find_element_by_xpath("//div[normalize-space()='My Button']") 

XPath expression for variable text

In case the text is a variable, you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 
Active reading [<https://en.wiktionary.org/wiki/in_case#Conjunction> <https://en.wikipedia.org/wiki/XPath>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

In the HTML which you have provided:

<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

IncaseIn case the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 

or at the end:

<div>My Button </div> 

or at both the ends:

<div> My Button </div>  

In these cases you have 2two options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

     my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

     driver.find_element_by_xpath("//div[normalize-space()='My Button']]") 

xpathXPath expression for variable Texttext

IncaseIn case the text is a variable, you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 

In the HTML which you have provided:

<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

Incase the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 

or at the end:

<div>My Button </div> 

or at both the ends:

<div> My Button </div>  

In these cases you have 2 options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

     my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

     driver.find_element_by_xpath("//div[normalize-space()='My Button']]") 

xpath for variable Text

Incase the text is a variable you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 

In the HTML which you have provided:

<div>My Button</div> 

The text My Button is the innerHTML and have no whitespaces around it so you can easily use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']") 

Note: text() selects all text node children of the context node


Text with leading/trailing spaces

In case the relevant text containing whitespaces either in the beginning:

<div> My Button</div> 

or at the end:

<div>My Button </div> 

or at both the ends:

<div> My Button </div> 

In these cases you have two options:

  • You can use contains() function which determines whether the first argument string contains the second argument string and returns boolean true or false as follows:

     my_element = driver.find_element_by_xpath("//div[contains(., 'My Button')]") 
  • You can use normalize-space() function which strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string as follows:

     driver.find_element_by_xpath("//div[normalize-space()='My Button']]") 

XPath expression for variable text

In case the text is a variable, you can use:

foo= "foo_bar" my_element = driver.find_element_by_xpath("//div[.='" + foo + "']") 
Updated answer
Source Link
undetected Selenium
  • 194.5k
  • 44
  • 304
  • 387
Loading
Source Link
undetected Selenium
  • 194.5k
  • 44
  • 304
  • 387
Loading