How to construct an xpath to click the logout link as per the html provided

Multi tool use
Multi tool use


How to construct an xpath to click the logout link as per the html provided



I am trying to click on the logout link to log out of the application using selenium xpath. The web page has user image link and when it is clicked a drop down for Logout link is displayed. When I try to locate the xpath of the Logout link, it does not go anywhere. Any thoughts, please ?



Here is my page source for that element. Don't know if this gives any idea.







Probably the the html is only there when you click on the down arrow button?
– Tarun Lalwani
Jul 3 at 4:51





What do you mean "does not go anywhere"? You get exception or just nothing happens?
– Andersson
Jul 3 at 5:03





Sorry, I mean when I right click on the element, it highlights the <body> tag but not the element path, as if the element is not there at all.
– 9009
Jul 3 at 5:09





@9009 Which Selenium Language Binding Art are you using? Java / Python / C# / NodeJS ?
– DebanjanB
Jul 3 at 5:58






I am using Java.
– 9009
Jul 3 at 5:59




2 Answers
2



As per the HTML you have shared to click on the user image you need to induce WebDriverwait for the next desired element to be clickable and you can use either of the solutions:



partialLinkText:


partialLinkText


new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("QA@some.com"))).click();



cssSelector:


cssSelector


new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.user a.user-menu img[src*='data:image/png;base64,']"))).click();



xpath:


xpath


new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='user']//a[@class='user-menu']//img[contains(@src,'data:image/png')]"))).click();





Thank you very much - I am able to click the link of the user profile and see the "Logout" link but unable to click the Logout link. So, when I click the user profile link : "QA@some.com", a drop down is displayed and that has "Logout" link. Clicking the Logout link, the application logs out. But inspect element points the <div> tag of the html. It won't point to the path of the element.
– 9009
Jul 3 at 6:41






@9009 Can you please raise a new question with your new requirement . Stackoverflow volunteers will be happy to help you out.
– DebanjanB
Jul 3 at 6:44





Sure, adding another one.
– 9009
Jul 3 at 6:46





I have added a new question. Here is the link : stackoverflow.com/questions/51148289/…
– 9009
Jul 3 at 6:56



Try this xpath:


//a[contains(., 'QA@some.com')]





I just tried it, It doesn't recognize the element. I have tried this way "//img[contains(@src,'image/png')]" and your way, both are failing.
– 9009
Jul 3 at 5:00





Does element visible when you try to interact with it?
– Andrei Suvorkov
Jul 3 at 5:03





The image does not even get clicked by it. My script has .click tied to this element, to click, when it is visible. However, the solution does highlight the element in the page source.
– 9009
Jul 3 at 5:14






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

BdByd8dQ Xxg5hAPMP8rlXaAisnj,J,9MQzd61yNPdEqIzzsywItumK8 smNjSEQ7zqn V
Pvg8,WzEa5BHVjNsVpOu8jKeD7R Y6OPY,CgkEG0B oYH,2z0eKaaeYGGan,qpJWoSWoFEl0cJ OoAf0cA3JCdRKiQRc46t1,JJTX,WHpE6 R

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications