1

How do I read the text in below? svg seems to be a special case.

Tried the below, but of no help

//*[name()='svg']/title

//*[name()='svg' and @title='Test']

//*[name()='svg' and contains(@title,'Test')]

    <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 137 125" version="1.1">
        <title>Test</title>
        <desc>Created with Sketch.</desc>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user12635792
  • 57
  • 1
  • 8

1 Answers1

3

To extract the text from the <title> tag within the WebElement you can use either of the following Locator Strategies:

  • Using css_selector:

    svg>title
    
  • Using xpath and name():

    //*[name()='svg']/*[name()='title']
    
  • Using xpath and local-name():

    //*[local-name()='svg']/*[local-name()='title']
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Yes, I tried the xpath approach that you recommended as //*[name()='svg']/title, but it didn't work. :-( – user12635792 Aug 23 '20 at 08:48
  • @user12635792 I trust `name()` wasn't part of my answer but `local-name()` and moreover you haven't told us the error you are seeing. Additionally,did you check the References I have provided? – undetected Selenium Aug 23 '20 at 08:50
  • @user12635792 as recommended hierarchy in [Locator Strategies](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890) try using `css_selector` – Moshe Slavin Aug 23 '20 at 11:12
  • Yes @DebanjanB@user12635792, apparently, only //*[name()='svg'] or //*[local-name()='svg'] works and not //*[local-name()='svg']/title or //*[name()='svg']/title – user12635792 Aug 23 '20 at 12:09
  • 1
    If Selenium has a mechanism to bind an XML namespace prefix to a namespace, it'd be preferable to use that rather than defeating the namespace via a `local-name()` predicate. Note too that `title` is in the `http://www.w3.org/2000/svg` namespace and so will require similar treatment to that given to `svg`. (Default namespace declarations apply to the element on which they appear and all descendent elements for which no namespace is specified.) – kjhughes Aug 23 '20 at 13:46
  • Lovel, thanks @kjhughes, even the below worked based on your recommendations. //*[name()='svg']/*[name()='title'] Thus we have 2 working solutions 1. //*[name()='svg']/*[name()='title'] 2. svg>title Thanks a lot – user12635792 Aug 24 '20 at 03:35
  • @DebanjanB: Does Selenium have a mechanism for binding XML namespace prefixes to namespaces? I'd like to update [Defining namespaces in XPath](https://stackoverflow.com/a/40796315/290085) if it does. – kjhughes Aug 24 '20 at 04:28
  • @kjhughes You were right, as the `title` is within the `http://www.w3.org/2000/svg` namespace, we have to add the `name()` or `local-name()` predicate. Based on your edit, I have added the third option with `name()`. – undetected Selenium Aug 24 '20 at 10:10
  • @DebanjanB: Yes, but unfortunately AFAICT from their documentation, there is no support for binding XML namespace prefixes to namespaces in Selenium currently, so we're stuck with `name()` or `local-name()` work-arounds. Please let me know if you know of, or come to know of, any such mechanism there. Thank you. – kjhughes Aug 24 '20 at 12:25