2
<span class="left-menu-title selectorgadget_selected" data-xpal="xpath-verify-selected" style="">Admin</span>

How can I write an XPath or CSS expression? I tried the plug-ins does not work.

If anyone knows how to click an item from the left-side bar after log-in to the site will be a great help. it does not click, the script fails.

@FindBy(xpath = "//SPAN[@class='left-menu-title'][text()='Admin']") 
WebElement clickOnAdmin;

public WebElement adminClick() {
    return clickOnAdmin;
}
1
  • 2
    Could you give the HTML or URL? Commented Jan 8, 2018 at 3:10

4 Answers 4

4

There are multiple classes but you are checking left-menu-title only.

The case of a SPAN tag name may also be a problem depending on a driver.

Fixed version, using contains() (note that it is not an ideal class XPath check - you need the concat and normalize-space, strictly speaking):

//span[contains(@class, 'left-menu-title')][text()='Admin']
Sign up to request clarification or add additional context in comments.

3 Comments

I'm quite sure that at least in Python+Selenium //SPAN will return the same node as //span... Does Selenium behaves differently in other programming languages?
@Andersson yeah, really good point, it then depends on a driver's XPath support, updated the answer accordingly. Thanks!
This is way off-topic, but you might find this SEDE query interesting.
0

what @alecxe wrote is a very good pointer.

Usually when a website has a strong front-end code embedded with data+JS, you should use functionalities. Especially when absolute xpath does not work such as your case with data var on the front-end. "data-xpal="xpath-verify-selected"

Guru99 xpath fonctionalities

  1. also please verify if your application or website is not embedded with iFrames. if so please change iframe window.

  2. if you can provide the stackTrace Error. I assume you are talking about NullPointerException or NotFindElementException.

Comments

0

As per the HTML you have shared, the following code block must work :

@FindBy(xpath = "//span[@class='left-menu-title selectorgadget_selected' and contains(.,'Admin')]") 
WebElement clickOnAdmin;

public WebElement adminClick() {
    clickOnAdmin.click();
}

Note : As you named the function as adminClick(), assuming you want to invoke click() method on the WebElement clickOnAdmin, click() method won't return anything. hence you have to discard the return clickOnAdmin; statement as well.

Comments

0

I would suggest trying writing your XPATH as //span[contains(@class,'left-menu-title') and .='Admin']

And instead of just element.click(); use javascript executor click. Like how it's below:

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath("//span[contains(@class,'left-menu-title') and .='Admin']")));

Hope this works for you! Let me know.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.