Home > OS >  find element by xpath but only specified tags
find element by xpath but only specified tags

Time:08-17

I am trying to find element by xpath currently I am using this.

title = self.find_element_by_xpath(f"//*[starts-with(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'{search.lower().strip()}')]")

but its giving me any match in any of the tags, but I want the element of it matches only specified tags such as h1,h2,span etc.

I have tried doing this

title = self.find_element_by_xpath(f"//span|h1|h2|h3|h4|a[starts-with(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'{search.lower().strip()}')]")

but it doesnot work.

CodePudding user response:

This XPath,

//*[self::span or self::h1 or self::h2]

will select all span, h1, or h2 elements in the document.

  • Related