0

I tried to click on the link 'Бренд', but it didn't work. What did I do wrong?

driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText()  -works
driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText() - doesn't work. Why?

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    You have identically same lines of code, how is it possible that one works and other doesn't? – acikojevic Mar 05 '18 at 10:52
  • to be able to click an element it should be visible, and not 'covered' by another element. It's possible you need to add a delay before doing a click action, to make sure element is fully visible – Igor Milla Mar 05 '18 at 10:55

2 Answers2

1

It is pretty much possible that the line with WebElement.getText() would work but the line with WebElement.click() won't work as follows :

driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText() //works 
driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).click() //doesn't work. 

Explaination

The reason for this behavior is when the Client (i.e. the Web Browser) returns back the control to the WebDriver instance once 'document.readyState' equals to "complete" is achieved, it is quite possible that the intended WebElement is present (element is present but does not necessarily mean that the element is interactable) and visible i.e. (element is visible and also has a height and width that is greater than 0). So you were able to extract the text Бренд.

But the WebElement being present and visible but doesn't garuntees that the WebElement is also clickable i.e. interactable.

Here you can find a detailed discussion on 'document.readyState' equal to "complete"

Solution

To click on the WebElement with text as Бренд you have to induce WebDriverWait as follows :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='filter-title' and text()='Бренд']"))).click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

If I am able to extract the information correctly from your image, then the class is already a unique identifier. You can just use driver.findElement(By.className("brand")).click();.

This should work if the secondary class "brand" is not used anywhere else. If this doesn't work then just take the class of the div that contains it:

driver.findElement(By.className("initialDivClass")).findElement(By.className("brand")).click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
MMeholli
  • 1
  • 2