0

logout button has class and attributes as 9 other elements have so unable to find that element and it has one child element that is also difficult to be found. i tried many relative xpaths but couldn't find. please help me to find. absolute xpath of that element is .//*[@id='js_7q']/div/div/ul/li[12] i used index as 12 for relative xpaths but couldn't .

<li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_activ‌​ity_log"}"> 
<li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_nf_pr‌​eferences"}"> 
<li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_setti‌​ngs"}">
  • Can you share that portion of your code and the error stacktrace? This will help us give you a better answer. – Swagin9 May 8 '17 at 15:54
  • <li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_activity_log"}"> <li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_nf_preferences"}"> <li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_settings"}"> – anusha raju May 8 '17 at 16:19
  • code is shown below for 4 elements like this 5 more elements have (4th one is my target) <li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_activity_log"}"> <li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_nf_preferences"}"> <li class="_54ni navSubmenu __MenuItem" role="presentation" data-gt="{"ref":"async_menu","logout_menu_click":"menu_settings"}"> – anusha raju May 8 '17 at 16:25
  • sorry you many not get that code perfectly i don't know how to edit perfectly . code is started with <li class="_54ni navSubmenu __MenuItem" for each element. – anusha raju May 8 '17 at 16:32
  • Do you have the selenium code block and error message? This looks like it's just the webpage source code, correct? – Swagin9 May 8 '17 at 16:40
1

Use this xpath to get the facebook logout element

//form[@id='show_me_how_logout_1']/ancestor::a

In this case it seems to be easier to find the underlying form element and then getting the link ancestor then trying to find a child element.

  • how could u find that xpath with use of ancestor ,, in entire page source code there is no id for any element and what is that "form" is it a tag? @quinny Thank you very much. – anusha raju May 9 '17 at 13:01
  • Not sure what tool you are using for your xpath work, but I highly recommend chrome dev tools. If you drill down more into the line item of the logout line object, it has a form object underneath of it under a couple spans. Remember you may need to open the dropdown that includes the login link for it to populate the DOM, I am not sure. That form had a unique id, then the next step is to just go up from there. When you need to jump multiple parents use ancestor instead, that 'a' object is the link for the logout. – quinny1187 May 9 '17 at 14:56
  • Anusha, you need to learn how to create XPath and CSS. You can create without using any tool. If wanna learn basis usage of xpath and selenium use following link: [link]testarenablog.wordpress.com/2016/11/04/css-selector-in-selenium [link]testarenablog.wordpress.com/2016/08/14/xpath-in-selenium – saurabhguptacse May 9 '17 at 17:26
  • working on selenium , my doubt is ,there is no element with "form" tag in that source code how could u create that xpath? with selenium webdriver itself?? or any other tool? – anusha raju May 10 '17 at 10:32
  • The problem is the Log Out form does not get generated in the DOM until you click the dropdown in the nav that exposes it. If you search the page for Log Out text without clicking the dropdown in navbar you get 0 results. Opening it first, then searching for Log Out text gives the result you are looking for. Remember not everything in the entire page gets loaded immediately when the page is opened. – quinny1187 May 10 '17 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.