0

Hope someone can guide me in the right direction of clicking the 'Active' checkbox , Problem is application defaults to have Active checked so filter dropdown stores the option 'Active' so my xpath sees two 'Active' and clicks the first one which is the dropdown.

driver.findElement(By.xpath("//span[contains(text(),'Active')]")).click();

Screenshot of App

HTML pic of the dropdown breakdown

When I used code above two elements were present at the same time so dropdown got clicked instead of actually uncheck the 'Active' checkbox. Hope this makes it clear for you.

4 Answers4

0

When the text is not unique you can add something that makes the xpath unique - some parent or descendant element or more attributes of the wanted element.

  driver.findElement(By.xpath("//label[@class='ant-checkbox-wrapper']//span[contains(text(),'Active')]")).click();
K. B.
  • 3,342
  • 3
  • 19
  • 32
0

If this is present twice, then using xpath indexing you can differentiate :

(//span[contains(text(),'Active')])[1]

or

(//span[contains(text(),'Active')])[2]
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • XPath with indexes are not among the best practices when using Selenium. – undetected Selenium Sep 01 '21 at 14:32
  • @DebanjanB : Yes I know, but there wasn't enough HTML shared by OP so that we can distinguish between 2 nodes. – cruisepandey Sep 01 '21 at 14:33
  • Won't deny that, however there is a lot of difference between the 2 texts **Active** and **Inactive** – undetected Selenium Sep 01 '21 at 14:52
  • I still fail to understand, `//span[text()='Active']` is present 1/2 in HTML DOM, and we do not want first element, and we do not have sufficient HTML, so what would be the best way to give answer. Also I got a feedback from `Johan Lindstrom` that it worked great ! – cruisepandey Sep 01 '21 at 14:54
0

Instead of partial text match using contains() you can use the exact text match and you can use the following Locator Strategies:

  • Active:

    driver.findElement(By.xpath("//span[text()='Active']")).click();
    
  • Inactive:

    driver.findElement(By.xpath("//span[text()='Inactive']")).click();
    

However, as the element is a React Native element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Active:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Active']"))).click();
    
  • Inactive:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Inactive']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • How does this answer the question ? There are two active web element on the UI. Read carefully. – cruisepandey Sep 01 '21 at 02:37
  • @cruisepandey I don't understand your comment/question. Yes, two active web elements, so can you rephrase your question about what you are asking? – undetected Selenium Sep 01 '21 at 14:31
  • You are using `//span[text()='Active']` and there are 2 web elements with this xpath, and eventually the first web element OP does not want it, so I had a question, how does it help ? – cruisepandey Sep 01 '21 at 14:32
  • @cruisepandey There was a typo in the second part of the answer which I have fixed and should address your concern. – undetected Selenium Sep 01 '21 at 14:34
0

if it more than one element found, always you are finding it in same position. better we should do for eg: following example pointing to 2nd position.

WebDriverWait wait = new WebDriverWait(driver, 20);
By appct = By.xpath("//span[text()='Active']");
wait.until(ExpectedConditions.elementToBeClickable(appct));
driver.findElements(appct).get(1).click();
System.out.println("Clicked the accept checkbox");
Jayanth Bala
  • 758
  • 1
  • 5
  • 11