0

I am new to this and I am trying to understand the usage of selenium and XPath

string exp = "//*[@id=\"g_1_bHwVovAN\"]/td[2]";
var dateTime = chromeDriver.FindElementsByXPath(exp);

With this code, I can only take 1 element. How can I change this "bHwVovAN" part, so I can reach the all that kinds of elements on the website.

string exp = "//*[@id=\"g_1_[^[0-9+]]\"]/td[2]";
var dateTime = chromeDriver.FindElementsByXPath(exp);

I tried to use regex, but It did not work. It did not recognize regex parts. Also, I looked at the other posts, and tried to use matches, and also did not work. How can I solve it?

If I did not write clear and correct way, I am also new to English. Sorry

Kaan Taha Köken
  • 933
  • 3
  • 17
  • 37
  • 1
    To use regex, don't you need to use `fn:matches`? Otherwise, is is doing an equality match, and: *that won't match via equality*. Like: https://stackoverflow.com/a/10404321/23354. As a complete guess, maybe: `string exp = "//*[matches(@id, 'g_1_[^[0-9+]]')]/td[2]";` ? – Marc Gravell Jul 05 '17 at 09:51
  • @MarcGravell i tried to use that template but it did not work [link]https://stackoverflow.com/questions/21405267/xpath-using-regex-in-contains-function – Kaan Taha Köken Jul 05 '17 at 09:57

1 Answers1

1

There is a matches() function in XPath 2.0 that would solve your issue, but selenium doesn't support this XPath version.

You can try below to match elements with id attribute that starts with "g_1_":

string exp = "//*[starts-with(@id,\"g_1_\")]/td[2]";
Andersson
  • 51,635
  • 17
  • 77
  • 129