Selenium ... lost in Xpath

65 views
Skip to first unread message

Alexander Schwan

unread,
Apr 26, 2018, 6:42:18 AM4/26/18
to Selenium Users
Hi Guys,

I have an issue im seriously struggeling with. I want to select an element via Xpath which looks like this:

<div class="u8-contextmenu-item u8-contextmenu-parentitem-selection" id="n29" data-helptext="Companies" data-helpuri="help://dom" style="display: block;"><div class="u8-contextmenu-item-image" style="width: 16px;" data-size="s"><span class="img u8-img-infoarea-fi-s"></span></div><div class="u8-contextmenu-popup-image"><span class="img u8-img-arrow-r-xs"></span></div><div class="u8-contextmenu-item-text">Companies</div></div>


Be aware, that id="n29" is dynamically generated, so I cant rely on the ID. Anyway, if randomly the ID in the code matches the generated ID, the following selection works:

driver.findElement(By.xpath("//*[@id=\"n29\"]")).click();

what also works, is when i make the path absolute in chrome via

$('*').removeAttr('id')

driver.findElement(By.xpath("/html/body/div[4]/div[3]/div[2]/div/div/div/div[1]")).click(); <-- this one works too

So, I checked out a bunch of other selections to make the process more stable, e.g.

driver.findElement(By.xpath("//div[@class=\"u8-contextmenu-item u8-contextmenu-parentitem-selection\" and @data-helptext=\"Companies\"]"));
driver.findElement(By.xpath("//div[@class='u8-contextmenu-item u8-contextmenu-parentitem-selection' and @data-helptext='Companies']"));
all those xpaths works in chrome development tool if I search for them but they are not working in seleium, the error message is always:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="u8-contextmenu-item u8-contextmenu-parentitem-selection" and @data-helptext="Companies"]"}
  (Session info: chrome=65.0.3325.181)

Do you guys have any clue what might cause the issue?

Thanks,

Alex


Santhu

unread,
Apr 26, 2018, 7:57:36 AM4/26/18
to seleniu...@googlegroups.com
Try with xpath "//div[contains (text (),'Companies')]".

Also, check if the element is inside an iframe or page is taking some time to load.

Regards,
Santhosh

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/3481da63-c851-483a-b7fb-eae84aecac7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Schwan

unread,
Apr 26, 2018, 9:27:20 AM4/26/18
to Selenium Users
Hi,

thanks for your response. It does not work :/

I dont understand why it works if the ID matches randomly with the one in the code and this one works:  /html/body/div[4]/div[3]/div[2]/div/div/div/div[1] while none of the other solutions work?

Can an iFrame or PageLoad issue then really be the issue?

Regards,

Alex

Erich Siffert

unread,
Apr 27, 2018, 7:28:38 AM4/27/18
to Selenium Users
Hi Alex

yes of course!
If the element you want to fetch is loaded within another iframe / frame you will have to switch over with

driver.switchTo().frame(...)

...if it is really the case that the element you want to retrive is in another frame, this would explain the NoSuchElement exception as well...

another possibility:
if you have an xPath like:

driver.findElement(By.xpath("//div[@class='u8-contextmenu-item u8-contextmenu-parentitem-selection' and @data-helptext='Companies']"));

check what the HTML SOURCE really looks like the xPath clause.
Especially "class" attributes might be displayed differently from browser tools (order of CSS, additional blanks/more or less blanks etc.)
It might be that "class='u8-contextmenu-item u8-contextmenu-parentitem-selection'" is not what it appears to be, it might be:
"class='  u8-contextmenu-parentitem-selection     u8-contextmenu-item   '" or whatever...

eventually this works better (well, not performance-wise):
//div[contains(@class, 'u8-contextmenu-item') and contains(@class, 'u8-contextmenu-parentitem-selection']

cheers
Erich

Yogesh Khachane

unread,
Apr 27, 2018, 11:44:49 PM4/27/18
to seleniu...@googlegroups.com
Hi Alex,

You can use below xpath

//div[text()='Companies']//preceding::div[@data-helptext='Companies'][1]



--
With Best Regards,
Yogesh Khachne
Framework Developer
+91-9922455800


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1de20c08-4ac3-4953-a9b0-faf70e92a447%40googlegroups.com.

Mohanraj K

unread,
Apr 27, 2018, 11:47:53 PM4/27/18
to seleniu...@googlegroups.com
Hi..,
I think ur class is hidden u not able to perform Operations over these div..i need to know full scenario for help u out..


Style=display:block
If u search some word but the word not matches so it act as hidden one..plz let me know the full scenario..

On Fri, Apr 27, 2018, 5:07 AM Mohanraj K <mohanraj....@chainsys.com> wrote:
driver.findelement(By.xpath(".//*(text()='Companies')")


On Fri, Apr 27, 2018, 4:59 AM Mohanraj K <mohanraj....@chainsys.com> wrote:
Hey bro ur class called on many places means it wont works.
Just use this 
diver.findelement(By.xpath(".//*(text()='Companies'))


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/16ee5b56-419c-49e5-a539-4bca021b5584%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


----
Disclaimer:
This email and any attachments to it may be confidential and are intended solely for the use of the individual to whom it is addressed.

subha a

unread,
May 4, 2018, 3:05:28 AM5/4/18
to Selenium Users
XPath is defined as XML pathIt is a syntax or language for finding any element on the web page using XML path expression. XPath is used to find the location of any element on a webpage using HTML DOM structure. The basic format of XPath is explained below with screen shot.

Syntax for XPath:

XPath contains the path of the element situated at the web page. Standard syntax for creating XPath is. 

Types of X-path

1) Absolute XPath .

2) Relative XPath .


Selenium training in chennai

Reply all
Reply to author
Forward
0 new messages