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;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Syed Alam
  • 35
  • 3

4 Answers4

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']
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    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 Jan 08 '18 at 16:47
  • 1
    @Andersson yeah, really good point, it then depends on a driver's XPath support, updated the answer accordingly. Thanks! – alecxe Jan 08 '18 at 17:46
  • This is way off-topic, but you might find [this SEDE query](http://data.stackexchange.com/stackoverflow/query/780210/users-by-number-of-profile-trackable-badges-obtained) interesting. – E.P. Jan 09 '18 at 14:37
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.

Ya Yan
  • 63
  • 13
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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
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.

S K
  • 308
  • 2
  • 6
  • 20