How to click a link(href) that matches partially?

10,303 views
Skip to first unread message

jyo rani

unread,
Apr 10, 2012, 9:05:30 PM4/10/12
to webdriver
Actually the url in the source code is - '/ccc/ctxxxx/Load.xxx?
ID=54354355 and the Id value changes everytime based on the input.

What i am looking to do is , if the url matches partially then click
on the link..but as i removed id value below line is not working.Any
ideas how to click this href with partial match?

driver.findElement(By.xpath("//a[contains(@href, '/ccc/ctxxxx/Load.xxx?
ID')]")).click();

Tarun Kumar

unread,
Apr 11, 2012, 12:58:57 AM4/11/12
to webd...@googlegroups.com
hmm could you post html source code of your URL 
also could you try this -

driver.findElement(By.xpath("//a[contains(@href, '/ccc/ctxxxx/Load.xxx')]")).click();

also you can test your xpath locator in firebug/chrome console using -

$x("//a[contains(@href, '/ccc/ctxxxx/Load.xxx')]")

darrell

unread,
Apr 11, 2012, 9:22:31 AM4/11/12
to webdriver
The findElement example you provided should have worked. I'm assuming
it fails to find the element. If this is not true or you aren't sure
you might want to break the code into two lines, i.e.

WebElement anchor = driver.findElement(By.xpath("//a[contains(@href,'/
ccc/ctxxxx/Load.xxx?ID')]"));
anchor.click();

If it fails on the second line then you know the problem is not
finding the element but clicking it.

If it fails to find the element, the only thing I can guess is that
whatever WebDriver sees is not the same as what you think it sees.
Occasionally, the tools we use to examine a web page don't show us
exactly the same as what WebDriver sees. In these cases I'll write a
little snippet which will find what I'm looking for and examine it.
For example,

List<WebElement> anchors = driver.findElements(By.tagName("a"));

After this line of code I'd set a breakpoint and run to breakpoint.
Once halted at the breakpoint I can use the watch window to examine
the contents of 'anchors'. I put anchors.size() in the watch window
then I examine anchors.get(n).getAttribute("href") where n is from 0
to anchors.size()-1. Alternatively, I could just write the program:

List<WebElement> anchors = driver.findElements(By.tagName("a"));
Iterator<WebElement> i = anchors.iterator();
while(i.hasNext()) {
WebElement anchor = i.next();
String href = anchor.getAttribute("href");
System.out.println(href);
}

From the output of this I might see why my assumptions about the href
attribute are wrong. Could the findElement match more than one
WebElement? Not sure what would happen there.

Darrell

P.S. you might also want to use
driver.findElement(By.cssSelector("a[href*='/ccc/ctxxxx/Load.xxx?
ID'")); rather than an xpath. This is more writing efficient code and
nothing to do with your actual problem.

Meenakshi Iyer

unread,
Jan 21, 2014, 12:57:50 AM1/21/14
to webd...@googlegroups.com
Hi Darrell,

Thanks for your insight on the query it was useful. Though in my case the element is not to be found even after listing the "href" links on the page, does it mean it is a hidden link.Also, the link that i am interested in, is a security certificate error "Continue to this website (not recommended)." Would appreciate your thought on it.

Error encountered: Exception in thread "main" java.lang.NullPointerException

Thanks,
Meenakshi

Krishnan Mahadevan

unread,
Jan 21, 2014, 8:50:12 AM1/21/14
to webdriver
Meenakshi,

I am guessing that you are trying to work with IE and that is where you are seeing this certificate error. I dont think you should write WebDriver code to deal with cert exceptions. You should instead add the certificate as an exception into IE on a one time basis and then work with it. IMO that is the right way of dealing with cert exceptions on IE

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/


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages