Selenium ... lost in Xpath

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Selenium ... lost in Xpath

Alexander Schwan
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


--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Selenium ... lost in Xpath

Santhu
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

On Thu, Apr 26, 2018, 4:12 PM Alexander Schwan <[hidden email]> wrote:
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


--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAGPWr3hMJixddGZQW-Y9Ac7_MULgkGavMqsxJeXomJdUMwdMvg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Selenium ... lost in Xpath

Alexander Schwan
In reply to this post by Alexander Schwan
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Selenium ... lost in Xpath

Mohanraj K
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 <[hidden email]> wrote:
driver.findelement(By.xpath(".//*(text()='Companies')")


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


On Thu, Apr 26, 2018, 6:57 PM Alexander Schwan <[hidden email]> wrote:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CA%2BqnjqGuGb4502CGykmQ%2BoQE9mHeuN286os35WFkpaCvwmHZTw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Selenium ... lost in Xpath

Erich Siffert
In reply to this post by Alexander Schwan
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

On Thursday, April 26, 2018 at 3:27:20 PM UTC+2, Alexander Schwan wrote:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1de20c08-4ac3-4953-a9b0-faf70e92a447%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Selenium ... lost in Xpath

Yogesh Khachane
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


On Fri, Apr 27, 2018 at 4:58 PM, Erich Siffert <[hidden email]> wrote:
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

On Thursday, April 26, 2018 at 3:27:20 PM UTC+2, Alexander Schwan wrote:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1de20c08-4ac3-4953-a9b0-faf70e92a447%40googlegroups.com.

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAJOfxvOxvcw%3D4hJZ%3DGZ2RdRDJhy%3DcEUBv6jZW3mULxFxvDg%3D_w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Selenium ... lost in Xpath

subha a
In reply to this post by Alexander Schwan
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1188f76d-14a6-4061-8b24-e46c1f24c477%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.