0

I have the following WebElement which can locate using xpath:

/html/body/div/div/div/div[2]/div[1]/div[4]/div/div/div[335]/div[1]/span

In the above locator, div[335] number randomly changes. I want to use regex and use the xpath as element locator.

Can anyone please let me know the regular expression to use with the above xpath?

I'm using Selenium Webdriver with Java. IDE is Intellij and I'm testing on Chrome browser.

I'm going to use the above mentioned xpath as below:

@FindBy(how = How.XPATH, using =/html/body/div/div/div/div[2]/div[1]/div[4]/div/div/div[335]/div[1]/span

public WebElement totalSuppliers;
iamdanchiv
  • 4,052
  • 4
  • 37
  • 42
user1767083
  • 157
  • 1
  • 3
  • 10
  • 3
    I could not work out your task here. Why do you need to use regex? And anyway, might there be a better way to find any steps in your long xpath, such as ID or class name? An xpath like this is extremely brittle, a minor change in the webpage can break it and it will be very hard to debug. – Mikhail Ramendik May 30 '17 at 14:25
  • 1
    what changes the 335? or why is it changed? you'll need to generate a dynamic selector more information would be helpful –  May 30 '17 at 14:27
  • Does this help https://stackoverflow.com/questions/1398705/how-to-use-regex-in-selenium-locators – dumbPotato21 May 30 '17 at 14:28
  • 1
    good link for using xpaths http://test-able.blogspot.ie/2016/04/xpath-selectors-cheat-sheet.html –  May 30 '17 at 14:31
  • This question would have been resolved in no time if you (OP, **user1767083**) would have added a brief **HTML snippet** of the element you are targeting and it's enclosing containers. – iamdanchiv May 30 '17 at 15:08
  • simply try :=/html/body/div/div/div/div[2]/div[1]/div[4]/div/div//div/div[1]/span – Kushal Bhalaik May 30 '17 at 16:35
  • @iamdanchiv below is the html snippet
    Total number of suppliers : 333
    Total
    60,407,905.98
    64,775.40
    – user1767083 May 31 '17 at 01:11
  • @ Lee92 335 is changing and it represent the no of records found when search – user1767083 May 31 '17 at 01:40
  • @ kushal: your solution is not working..... – user1767083 May 31 '17 at 04:07
  • Can you give us the complete DOM in a good readable format? – santhosh kumar May 31 '17 at 11:46

0 Answers0