Search code examples
pythonseleniumselenium-webdriverxpathplaceholder

Selenium - How to find an element by part of the placeholder value?


Is there any way to find an element by a part of the placeholder value? And ideally, case insensitive.

<input id="id-9" placeholder="some TEXT">

Search by the following function doesn't work

browser.find_element(by=By.XPATH, value="//input[@placeholder='some te']")


Solution

  • You can always use contains instead of equals, as following:

    browser.find_element(By.XPATH, "//input[contains(@placeholder,'some te')]")
    

    To make it case-insensitive you can use translate function, as following:

    browser.find_element(By.XPATH, "//input/@placeholder[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'some te')]")
    

    Case-insensitive contains Xpath is taken from this question