I am coding in java with selenium and i am stuck.
I have to find element by ID, the Id of the element is like this "msi_num_create-copy"
(the num is changing every time), there is a way to find the element by part of the Id?
Asked
Active
Viewed 278 times
1

cruisepandey
- 28,520
- 6
- 20
- 38

Yonatan Alshech
- 25
- 5
-
`//*[contains(@id, 'your_sub_string')]` , You also have the function `starts-with`, instead of `contains` – KunLun Jul 01 '21 at 11:38
1 Answers
2
yes you would need to use either css selector or xpath :
XPATH would be :
//*[contains(@id, '_create-copy') and starts-with(@id, 'msi')]
and use it like this :
WebElement someEle = driver.findElement(By.xpath("//*[contains(@id, '_create-copy') and starts-with(@id, 'msi')]"));
someEle.click(); or someEle.sendKeys("some string");

cruisepandey
- 28,520
- 6
- 20
- 38