1

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']")

Prophet
  • 32,350
  • 22
  • 54
  • 79
Mike
  • 347
  • 1
  • 2
  • 15

2 Answers2

4

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

Prophet
  • 32,350
  • 22
  • 54
  • 79
2

@Prophet's answer is good, but I wanted to add CSS case too since it may be helpful. You can use ^= to search for attributes starting with certain keyword and $= for attribute starting with certain keywrd and *= for attribute containing a keyword.

browser.find_element(By.CSS_SELECTOR, 'input[placeholder*="some te"]')

You can make it case-insensitive by adding i before closing the bracket

browser.find_element(By.CSS_SELECTOR, 'input[placeholder*="some te" i]')
zaki98
  • 1,048
  • 8
  • 13
  • Thank you! But is everything correct? I'm getting the error `selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified` – Mike Jan 02 '23 at 16:20
  • @Mike really sorry i had a syntax error and i used the wrong selector it should be working now i tested it – zaki98 Jan 02 '23 at 17:27