I have a problem that is very strange for me because the code that I wrote, I am quite sure should work!

Config Details

Chrome version: 80.0.3987.132

ChromeDrivers version: same as Chrome version 80 (80.0.3987.106)

OS: Windows 10

IDE: InteliJ

Programming language: Java API

TestURL: https://allegro.pl

I am currently testing , just for learning purpose, and so far everything worked. In general the basic approach to click a menu element is to use this code:

driver.findElement(By.xpath("//div[@data-box-name='Some Value']/div/ul/li[2]/a")).click();

Now I am in deeper menu category on this page: https://allegro.pl/kategoria/komputery and I want to click on "Drukarki i skanery" like this:


I know this is good, because when I do right click on this element and select copy Xpath it gives me almost this same Xpath. It gives me this:


I have tried with this Xpath as well and it does not work as well. I attached screen so you may see how the code looks like.


Is it possible that this entry is too deep and I have to give some longer path to my element? Please any help would be appreciated, I am kind of stuck on that right now.

  • Please make a try with this XPath //a[contains(text(),'Drukarki i skanery') and @class='_w7z6o _uj8z7 _1h7wt _1bo4a _6kfrx'] Mar 6, 2020 at 10:30
  • I can not use class definition like that because it is dynamic. During building process of productive app it may change. Also using text is not that great couse some advertisements may appear with this same text, and then it would be a problem. If used in AND operation like you did it make sense, but just text no.
    – ppaweluz
    Mar 6, 2020 at 11:02

1 Answer 1


What's the problem then? Both xPaths seem functional. On the other hand, a good practice is to simplify it as much as possible, so it's readable for you in the future and for other people. The following xPath seems a bit more readable to me:

//div[@id="opbox-category-tree"]//a[text()="Drukarki i skanery"]

You don't need to write every element on the way down as in section/div[2]/ul/li[3]/a. Also, [2], [3] etc. are not a good practice, it might change easily in the future, making your xPath disfunctional.

EDIT based on discussion below:

The real problem is that the test flow is as follows:

Given  as a user I opened page "https://allegro.pl/"
When agreement window appears
Then close agreement window
And  choose department Elektronika
And  choose category Komputery
And choose printer categories and scanners

When choose category Komputery is executed, it opens the category "Komputery" on a new browser tab, so you'd need to switch to that new tab first, and then look for "Drukarki i skanery" menu link.

  • I am aware that the order can change, but thank you for pointing that out. and what is the problem...well.. it simply does not work. I tried your solution, and mine and both do not work. I got all the time this same error - Unable to locate an element with the xpath expression. those are my attempts: driver.findElement(By.xpath("//div[@id=\"opbox-category-tree\"]//a[text()=\"Drukarki i skanery\"]")).click(); driver.findElement(By.xpath("//div[@data-box-name='Categories']]//a[text()='Drukarki i skanery']")).click(); Both do not work.
    – ppaweluz
    Mar 6, 2020 at 11:35
  • I know it seems not possible but it just does not work. Everything is on github, it you really want to check it on your own. github.com/izabeluz/Testing
    – ppaweluz
    Mar 6, 2020 at 11:41
  • @ppaweluz: how about if you change " for ': driver.findElement(By.xpath("//div[@id='opbox-category-tree']//a[text()='Drukarki i skanery']")).click();
    – pavelsaman
    Mar 6, 2020 at 11:44
  • It also does not work. Have tried in very different ways, still nothing...
    – ppaweluz
    Mar 6, 2020 at 11:48
  • 1
    It worked!! I did not know this concept! thank you very much for your time and your effort. You did save me, cheers !!
    – ppaweluz
    Mar 6, 2020 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.