-1
<span class="icon-holder">
            <i class="c-brown-500 ti-package"></i>
        </span>
        <span class="title">Vehicle Import</span>

There have a lot of classes with "title" name. I was trying to figure out the proper xpath for this one. I have tried the following xpaths. None of them work.

driver.findElement(By.xpath("//span[contains(@class,'title') and contains(text(),'Vehicle Import')]")).click();
driver.findElement(By.xpath("//span[@class='title']")).click();
driver.findElement(By.xpath("//span[@class='title' and text()='Vehicle Import']"));

Can you guide me how to write the proper one?

1
  • Why is the title hyphenated? Is the hyphenation related to the question is some way? Maybe I'm missing something... Jul 5, 2023 at 1:40

3 Answers 3

0

To locate the span element with the class "title" and the text "Vehicle Import" using Selenium in Java, you can try the following XPath expression:

driver.findElement(By.xpath("//span[@class='title' and text()='Vehicle Import']")).click();

This XPath expression specifies that you want to find a span element with the class attribute equal to "title" and the exact text content equal to "Vehicle Import".

Alternatively, you can also use the XPath expression with the contains() function to match partial text:

driver.findElement(By.xpath("//span[contains(@class,'title') and contains(text(),'Vehicle Import')]")).click();

This XPath expression finds a span element with the class attribute containing "title" and the text content containing "Vehicle Import".

By using one of these XPath expressions, you should be able to locate and interact with the desired span element successfully.

2
  • none of them work actually, is there any way that i can debug whether it is clicking that button or not? or if click what is happening? showing the following response in eclipse console "Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable (Session info: chrome=114.0.5735.198)"
    – Robi
    Jul 3, 2023 at 6:22
  • Can you confirm whether there is any overlay that is covering the button? Or if it is actually showing in the visible area of the screen? Does the button load or appear after another action? In that case may be add a wait to ensure the button loads properly. Jul 4, 2023 at 5:50
0

This is a challenging situation when we have multiple attributes inside a tag. I would recommend below locators to get out of this situation:

  • //span[normalize-space()='Vehicle Import']
  • //span[descendant::span[@class='title' and text()='Vehicle Import']]

When we encounter such kind of situations, we generally discuss them in our automation strategy meetings. Hope this information is helpful for you!!!!

0

These are the common issues faced by everyone in software qa company. Below XPath identifies the siblings of the context node.

//span/following-sibling::span[@class='title' and contains(text(),'Vehicle Import')]

By using above XPath, you should be able to locate the web element with the desired span successfully.

1

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.