0

Html:

<div class="component-multi-row-slide-title"> Cloth </div>

The xpath

/html/body/div/main/div/div/div[2]/div/div/div/div[1]/div/div***[4]***/div/div[2] the div[4] 

keep changing. If new item come into web, developer will change the div number.

I want to click on 'Cloth'. The xpath keep changing when new category is added.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
orkeddelilah
  • 83
  • 3
  • 9

6 Answers6

1

To start with using absolute xpath have certain disadvantages, as when new elements are added the xpath of the previously identified elements will keep on changing. Hence the solution would be to use Relative Xpath.

As per the HTML you have provided, to click on the element with text as Cloth you can use the following Locator Strategy:

  • XPath A:

    //div[@class='component-multi-row-slide-title' and contains(., 'Cloth')]
    
  • XPath B:

    //div[@class='component-multi-row-slide-title' and normalize-space()='Cloth']
    
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Absolute Xpath: It contains the complete path from the Root Element to the desire element.

Relative Xpath: This is more like starting simply by referencing the element you want and go from the particular location.

You use always the Relative Path for testing of an element. The reason behind that is if you make any architectural change in the website the change won't effect the testing or selecting of the element.

So Use relative xpath which is as follows

WebElement cloth = driver.findElement(By.xpath("//div[normalize-space()='Cloth']"));
cloth.click();

Let me know if this doesn't help

Rajagopalan
  • 5,465
  • 2
  • 11
  • 29
Mounika
  • 38
  • 7
0

I would recommend to use customized xpath. As given below:

driver.findElement(By.xpath("//div[contains(text(), 'Cloth')]").click

driver.findElement(By.xpath("//div[contains(text(), 'Fashion Acessories')]").click

It will work for sure. Feel free to let me know if something is wrong regarding xpath.

Thanks

khawar
  • 112
  • 1
  • 5
0

You need to use relative xpath in this case.

You can use any xpath from the below options. The first two will find the element by text and the 3rd option by class.

1>>driver.findElement(By.xpath("//div[contains(text(),'Cloth')]")).click();

2>>driver.findElement(By.xpath("//*[contains(text(),'Cloth')]")).click();

3>>driver.findElement(By.xpath("//div[@class='component-multi-row-slide-title']")).click();

Let me know if the same class name exist for other elements also.

0

Just for you reference: XPath is used to find the location of any element on a webpage using HTML DOM structure.

The disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.

So, in that case Relative Xpaths can be used:

//div[contains(text(),'Cloth')] or //*[contains(text(),'Cloth')]

//div[contains(text(),'Fashion Acessories')] or //*[contains(text(),'Fashion Acessories')]

How to use: driver.findElement(By.xpath("//div[contains(text(),'Cloth')]"));

Atul
  • 857
  • 8
  • 14
-1

You write the following code

driver.findElement(By.xpath("//div[normalize-space()='Cloth']").click

The above code will identify the element directly and also it would not be brittle in nature when developer changes the page elements.

Rajagopalan
  • 5,465
  • 2
  • 11
  • 29
  • its not working for two words example "Fashion Acessories" – orkeddelilah Oct 31 '19 at 04:43
  • I did not get your question then. You said you would like to locate the division which has the text Cloth which I have done. If you want to locate the div with the text `Fashion Acessories` then write `driver.findElement(By.xpath("//div[normalize-space()='Fashion Acessories']").click`. Your question is not clear enough to understand what you really want. – Rajagopalan Oct 31 '19 at 05:06
  • it only will work when add driver.findElement(By.xpath("//div[text()normalize-space()='Fashion Acessories']"). i need to add text() – orkeddelilah Oct 31 '19 at 10:18
  • This one is the wrong xpath `//div[text()normalize-space()='Fashion Acessories']`, I did not ask you to add text(). It would work perfectly if you just replace the string `Cloth` with `Fashion Acessories` – Rajagopalan Oct 31 '19 at 10:59