Skip to content

Instantly share code, notes, and snippets.

@shaunabandu
Created April 24, 2024 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunabandu/e0ccea16fe86da7d284ea72b553720ff to your computer and use it in GitHub Desktop.
Save shaunabandu/e0ccea16fe86da7d284ea72b553720ff to your computer and use it in GitHub Desktop.
Selenium xpath
//webpage under test: https://www.freemalaysiatoday.com/category/nation/2024/04/24/jpj-fines-3-bus-drivers-for-making-tiktok-videos-calls-while-driving/
//reference: https://www.guru99.com/xpath-selenium.html
//1. Basic Xpath
Xpath = "//summary[@class='list-none']";
//2. Contains()
Xpath = "//input[contains(@type, 'emai')]";
//3. AND or OR
XPath = "//*[@type='email' or @type='submit']";
XPath = "//*[@type='email' and @name='email']";
//4. Starts with
XPath = "//a[starts-with(@href, '/category')]";
//5. Text
XPath = "//h2[text()='Most Viewed Last 2 Days']";
//6. Ancestor
XPath = "//input[@type='email']//ancestor::div[@class='space-y-2']";
//7. Child
XPath = "//div[@class='flex flex-col space-y-4']//child::a[2]";
//8. Preceding
XPath = "//div[@class='flex flex-col space-y-4']//preceding::aside"; //Fails because aside comes before this div
Xpath = "//div[@class='flex flex-col space-y-4']//preceding::input"; //Passes because input comes after this div
//9. Following-sibling
XPath = "//div[@class='space-y-2']//following-sibling::button";
//10. Parent
XPath = "//div[@class='space-y-2']//parent::form";
//11. Descendant
XPath = "//main//descendant::form";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment