Xpath for Log-in - xpath

I am trying to click on "Log in" link present on home page of "stackoverflow" using xpath as show as below. But no success
driver.findElement(By.xpath("//a[contains(text(),'log in')]")).click();
Please help what i m missing here.
Thanks

Use the following xpath:
//a[#class='login-link'][text()='log in']
I tried personally and it worked with me. Hope it helps.

The xpath which you have used in your question returns two webelements:
​log in​​
​log in​​
that's why selenium is not able to click on that.So use
//a[#class='login-link'][text()='log in']

Try this :
driver.findElement(By.xpath("//a[#class='login-link' and .='log in']").click();
Above XPath match <a> tag having class attribute equals "login-link" and inner text equals "log in".
General tip: always prefer . over text(). There are very rare situations when you have to filter element specifically by using text().

Related

Struggling to write xpath / find a unique ID for an element

For web automation purpose, I need a unique ID of a text/label, which has some white space around it.
please check the screenshots attached
one screenshot shows - the best I could right the xpath but it doesn't solve the problem
another screenshot shows - my idea but not successful.
as per screenshot, I need xpath for text called "Suresh Kumar YOGESH"
Here is the xpath that you can use.
//div[#class='text_head_detail_view_name_holder']/h1/text()[2]
Screenshot: with sample code
To complete supputuri's answer, another option (might be more secure) :
//span[contains(.,"Offenders")]/following::text()[1]`

XPath for "Onclick" attribute

I have to perform a click event on one of the option in a drop down menu on a web page, here is my absolute path for the element.
Absolute Path:-
And I have tried Xpath to find the element:-
All the combination of Xpaths that I tried are not working.
Please do suggest me if you have any solution for the same?
Many Thanks,
Shruti
Try with these Xpaths:
//a[#data-original-title='Excel']
//a[#alt='Excel']
//a[text()='Excel']

Xpath or CSS : Trying to create either one

I am trying to capture "Featured Courses" using either an xpath or CSS selector on the following link qaclickacademy website. Below is what I have tried.
By.xpath("//div[contains(concat(" ",normalize-space(#class), 'text-center'), " Test ")]");
Would you like to help me, please?
If you use the xpath //ul[#class="gallery course-list"] you'll get all the licontaining courses.
If you want the link in each a element you should try this:
//ul[#class="gallery course-list"]//a/#href

Not able to identify element on Google Home page

I am trying to enter text on search text box on google home page. That I can identify using xpath as //*[#id="q"]. But I wanted to reach to that element using parent child relationship. I am using xpath as below :
`(//form[#id='a'])/div[2]/input[#id='q']`
But when I am running the script, it is giving error that said "no such element". Can someone please tell what I am missing in writing the xpath?
Please upload your HTML code to let us know of the issue better. Meanwhile, Try this .//form[#id='tsf']//input[#name='q'].
Hope it helps.

How to locate an element having href='#' attribute in anchor tag

abc
I am not able to locate above element. I tried //*[#id="contact-groups"], but with no success.
Well, XPath is not the best of the methods to find elements. But following will match the links with href = "#".
//a[#href="#"]
Not sure if your scenario is as simple as your question. I did test this with a simple html page.
The locator seems correct. You could try this too:
.//*[#id='contact-groups']
As per HTML code provided you can try below xpaths
//a[#id='contact-groups']
//a[contains(text(),'abc')]
//a[#href='#']
Thanks
I am guessing the link is in an Iframe.
Use the following code to switch to the frame and then click on the link
driver.switchTo().frame("frame-name");
driver.findElement(By.xpath("//a[#id='contact-groups']")).click();

Resources