Re: Help with finding xpath for a anchor tag

1,799 views
Skip to first unread message

Greg Arndt

unread,
Mar 1, 2013, 9:06:44 AM3/1/13
to seleniu...@googlegroups.com
Is this link within a collapsed div, thus making the element not visible?  If so, you will need to expand the containing div first to make the element visible.

On Thursday, February 28, 2013 4:43:41 PM UTC-6, Meena Molagavalli wrote:
Hi
I am trying to get to an anchor link in a page using selenium webdriver.I need to get the xpath for the Group Builder text But I am not able to detect using link. Could someone help me out?
I am also attaching a image with the tag details

Thanks
Meena

ASP

unread,
Mar 1, 2013, 11:03:30 AM3/1/13
to seleniu...@googlegroups.com
try this xpath 

//a[contains(.,'Group Builder')] 

Meena Molagavalli

unread,
Mar 1, 2013, 1:50:39 PM3/1/13
to seleniu...@googlegroups.com
Hi Greg, yes the element is visible. But when I try to click on it from the code, it hangs. I don't get any exception. I click after the page is loaded and only when the link is visible.

Meena Molagavalli

unread,
Mar 1, 2013, 1:51:32 PM3/1/13
to seleniu...@googlegroups.com
Hi ASP, I tried your suggestion, but it hangs for this line of code. I donot see any exception thrown.

ASP

unread,
Mar 1, 2013, 2:06:12 PM3/1/13
to seleniu...@googlegroups.com
post your  selenium and html code 

Meena Molagavalli

unread,
Mar 1, 2013, 2:09:13 PM3/1/13
to seleniu...@googlegroups.com

Hi All,
Let me explain a little more with the issue I have. I uploaded 2 files.
One file has the structure which used to work until recently. They made some changes with the UI and since then Selenium webdriver is seeing the issue of HANGING at the line of code where I need to click on the anchor link. For the picture "Working Structure.JPG" , I am able to get to the anchor tag, but for the "Needxpath.JPG" file I have a issue. Any suggestions would be helpful.
Regards
Meena
Needxpath.JPG
Working structure.JPG

ASP

unread,
Mar 1, 2013, 2:14:29 PM3/1/13
to seleniu...@googlegroups.com
you are saying something is hanging ... what is hanging ? browser  or there is an issue with HTML ? or you need selenium code to click on a link ? you havent posted ur selenium code 

Meena Molagavalli

unread,
Mar 1, 2013, 2:25:47 PM3/1/13
to seleniu...@googlegroups.com
I am using eclipse to run my testcases. This is the part of code  in bold which fails

public void openPage() {
        driver.switchTo().defaultContent();
        try {
            sleepSec(60,"Wating");
            driver.findElement(By.xpath("//a[contains(.,'Alert Builder')]")).click();
             
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

Neeraj Sharma

unread,
Mar 1, 2013, 2:28:58 PM3/1/13
to seleniu...@googlegroups.com
Your Xpath is wrong -
> --
> 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/msg/selenium-users/-/ge8U2ly0kv8J.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

ASP

unread,
Mar 1, 2013, 2:31:14 PM3/1/13
to seleniu...@googlegroups.com
when you say it fails is it printing any stack trace ? please post that too 

you are doing driver.switchTo.def... are you shifting from frame to main page ?  I'm guessing you are doing sleepSec(60,"waiting") is that wat causing webdriver to wait ? try removing that 

another suggestion for driver.findElement(By.xpath("//a[contains(.,'Alert Builder')]")).click(); 

try this driver.findElement(By.linkText("Alert Builder")).click(); 

see how that goes ... my doubt is on sleesec method ... i dont know its implementation probably thats making u think its haning 


ASP

ASP

unread,
Mar 1, 2013, 2:34:48 PM3/1/13
to seleniu...@googlegroups.com
another tip when you are working with xpath use xpath checker plugin in FF and see given xpath is retrieving desired element or not 


ASP

Meena Molagavalli

unread,
Mar 1, 2013, 2:45:48 PM3/1/13
to seleniu...@googlegroups.com
I tried but it still is not working. I changed the code to this:
public void openPage() {
        try {
            //driver.findElement(By.linkText("Alert Builder")).click();
            Wait<WebDriver> wait = new WebDriverWait(driver, 60);
                // Wait for search to complete
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(.,'Alert Builder')]")));
            driver.findElement(By.linkText("Alert Builder")).click();
             
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
But now, the code hangs at this location(bold line). It is not able to detect the Link. "The Alert Builder" link is already visible. As I mentioned, it used to work earlier with no issue. But now it selenium is not able to detect. I do use Firebug to verify the xpaths.

Meena Molagavalli

unread,
Mar 1, 2013, 2:46:35 PM3/1/13
to seleniu...@googlegroups.com
Any Suggestions Neeraj?

ASP

unread,
Mar 1, 2013, 3:20:04 PM3/1/13
to seleniu...@googlegroups.com
try following code 

public WebElement waitForElementToAppear(final By by, int waitFor) {

WebDriverWait wait = new WebDriverWait(webDriver, waitFor);

return wait.until(new ExpectedCondition<WebElement>() {

public WebElement apply(WebDriver input) {

stdout.println("Waiting for: " + by.toString());

WebElement element = webDriver.findElement(by);

stdout.println("Found: " + by.toString());

return element;

}

});

}


public void openPage() {
              waitForElementToAppear(By.xpath("//a[contains(.,'Alert Builder')]" , 30).click();    

}


Hope this works ... if it fails print stack trace

Meena Molagavalli

unread,
Mar 1, 2013, 4:22:47 PM3/1/13
to seleniu...@googlegroups.com
Thanks for the prompt responses ASP. I tried the code you suggested. looks like it was able to find the element but the click is happening before it is visible.
This is the console content:

Waiting for: By.xpath: //a[contains(.,'Group Builder')]
Found: By.xpath: //a[contains(.,'Group Builder')]
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 23.23 seconds
Build info: version: '2.29.0', revision: '58258c3', time: '2013-01-17 22:47:00'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1 build 2600 Service Pack 3', java.version: '1.6.0'
Session ID: f90a1c2d-455c-4913-911c-736c883fb67f

I tried to increase the wait, but still not working.

ASP

unread,
Mar 1, 2013, 4:38:27 PM3/1/13
to seleniu...@googlegroups.com
thats good now you know it does not hang ... it just cant find an element. do you capture screenshot ? 

I still dont have you html code so I'm not sure if there are any more hidden elements with 'group builder' text and selenium is trying to click them and fails. if thats the case then you have to change xpath. 


ASP

Meena Molagavalli

unread,
Mar 1, 2013, 4:43:22 PM3/1/13
to seleniu...@googlegroups.com
ASP, I already posted the code structure in above. Please let me know if you still have issue with viewing the snapshot.

ASP

unread,
Mar 1, 2013, 4:54:00 PM3/1/13
to seleniu...@googlegroups.com
I saw screenshots but all those divs are collapsed so I'm not sure what it contains.  are there any group builder text inside those div ? that get visible due to some condition ?

ASP

unread,
Mar 1, 2013, 4:57:27 PM3/1/13
to seleniu...@googlegroups.com
one more thing .. screenshot says Alert Builder and your code says group builder ? why ? 

Meena Molagavalli

unread,
Mar 1, 2013, 5:01:23 PM3/1/13
to seleniu...@googlegroups.com
No there are no more conditional based visibilities. Alert builder, Group Builder etc, these are links which I need to get to and the problem exists. Each div has a similar link. I have problem with all of these links. So the confusion. If I fix one, then I will have solution to all these links.

ASP

unread,
Mar 1, 2013, 5:09:51 PM3/1/13
to seleniu...@googlegroups.com
aaha so my guess was correct other divs also have same link. now can you tell me are those links visible on front end ? 

you have to tweak your xpath to reach to correct element ... I really cant do much seeing at screenshot so all the best 


ASP

Meena Molagavalli

unread,
Mar 1, 2013, 5:20:54 PM3/1/13
to seleniu...@googlegroups.com
Yes, those divs have links but different text and all show up on the front end. So when you mean tweak your xpath, may I know what can be done? do you want to see the expanded screenshot of other links?

ASP

unread,
Mar 1, 2013, 5:47:02 PM3/1/13
to seleniu...@googlegroups.com
is that web portal live ?  that will help more 

MM

unread,
Mar 1, 2013, 6:00:40 PM3/1/13
to seleniu...@googlegroups.com
No this not for public. It is an internal website.

Krishnan Mahadevan

unread,
Mar 1, 2013, 9:34:17 PM3/1/13
to seleniu...@googlegroups.com
With whatever XPath you have, can you please run a driver.findElements() and print the size of the WebElement list? That should give a clear cut confirmation on whether the XPath resolves to more than one WebElement. 

I also saw a ElementNotVisibleException. Selenium is still seeing the Element as Hidden here. 

So you could try doing one of the following :
1. Perform an action which would cause the Element to become visible. 
2. Use Javascript to flip the element into being visible. 
--
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/msg/selenium-users/-/JJLBqTSTj60J.

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


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

MM

unread,
Mar 2, 2013, 12:23:55 AM3/2/13
to seleniu...@googlegroups.com
Hi Krishnan
Thanks for you response. How can I use a Javascript to flip the element into being visible.?
I will try out the other tips you gave.
Regards
Meena

On Friday, March 1, 2013 6:34:17 PM UTC-8, Krishnan wrote:
With whatever XPath you have, can you please run a driver.findElements() and print the size of the WebElement list? That should give a clear cut confirmation on whether the XPath resolves to more than one WebElement. 

I also saw a ElementNotVisibleException. Selenium is still seeing the Element as Hidden here. 

So you could try doing one of the following :
1. Perform an action which would cause the Element to become visible. 
2. Use Javascript to flip the element into being visible. 

On Saturday, March 2, 2013, MM wrote:
No this not for public. It is an internal website.

On Friday, March 1, 2013 2:47:02 PM UTC-8, ASP wrote:
is that web portal live ?  that will help more 

--
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/msg/selenium-users/-/JJLBqTSTj60J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Neeraj Sharma

unread,
Mar 3, 2013, 7:15:17 AM3/3/13
to seleniu...@googlegroups.com
HI,
Now i am confused about ur current problem can you please tell us the problem  and exception and html and code again.

David Lai

unread,
Mar 3, 2013, 12:56:15 PM3/3/13
to seleniu...@googlegroups.com
wow, this thread has been going on for a bit.  At a certain point, it is easier to ask devs to add in a test hook to allow you to easy perform the action than to try automating it.

Manoj Hans

unread,
Mar 4, 2013, 1:58:11 AM3/4/13
to seleniu...@googlegroups.com
As per my understanding there is 2 links ie Group Builder and Alert builder.
After click on Group builder then Alert builder link is visible...m i right?
If wrong then i want to know about
1. Selenium version
2. Browser version

If yes then first u need to click on group builder then click on Alert builder.

First get all the group builder links on the page using 
List<WebElement> Links=driver.findElements(By.linkText("Group Builder"));
    System.out.println(Links.size());

If the links more then one then u can click on your desire link using
Links.get(1).click();


--Manoj Hans
Reply all
Reply to author
Forward
0 new messages