Home > front end >  How to find Span text Using Selenium Python?
How to find Span text Using Selenium Python?

Time:06-23

Why span element text is not coming here attaching the main div content here I am trying to pinpoint My work here but I am getting No element found what's wrong here how can I store this Myvalue element so that i can give click command on it , pardon my English,also if any better alternatives approach available please suggest

main_div = driver.find_elements(By.CLASS_NAME, "sub-menu")
# print(main_div)
for i in main_div:
    for span in i.find_element(By.XPATH,'.//span[@class = "leaf_line_item"]'):
        print(span)

Html code below fetched from main_div

<li >Home</li>



<li 
    onclick="executeNavigationAction('My Inbox','plm/workspace/myWorkspace.jsp?activeTab=My Inbox','false');"><span
        >My Inbox</span></li>






<li 
    onclick="executeNavigationAction('My Work','plm/workspace/myWorkspace.jsp?activeTab=My Work','false');"><span
        >My Work</span></li>






<li 
    onclick="executeNavigationAction('My Timesheet New','plm/workspace/myWorkspace.jsp?activeTab=My Timesheet New','false');">
    <span >My Timesheet</span></li>



<li 
    onclick="executeNavigationAction('My Calendar','plm/workspace/myWorkspace.jsp?activeTab=My Calendar','false');">
    <span >My Calendar</span></li>






<li 
    onclick="executeNavigationAction('My Attendance','plm/workspace/myWorkspace.jsp?activeTab=My Attendance','false');">
    <span >My Swipe Hours</span></li>






<li 
    onclick="executeNavigationAction('My Meetings','plm/workspace/myWorkspace.jsp?activeTab=My Meetings','false');">
    <span >My Meetings</span></li>






<li >My Details</li>



<li ><span style="cursor: pointer; cursor: hand;"
        onclick="executeNavigationAction('detailsNodeIdPM','ngtb/resource/resourceContainer.jsp?userId=6fih1mq0be20001000030ag19l&amp;module=PGM','false');">My
        Profile</span>




    <ul  style="display: none;top:-45px">
        <li >My Profile</li>


        <li 
            onclick="executeNavigationAction('delegationNodeIdPM','ngtb/delegation/delegationContainer.jsp?module=PGM','false');">
            <span >Delegation</span></li>


    </ul>
</li>




<li ><span style="cursor: pointer; cursor: hand;"
        onclick="executeNavigationAction('settingsNodeIdPM','plm/user/userPreferences.jsp?callingMode=leftTree&amp;userId=6fih1mq0be20001000030ag19l&amp;module=PGM','false');">My
        Settings</span>




    <ul  style="display: none;top:-45px">
        <li >My Settings</li>


        <li 
            onclick="executeNavigationAction('updateSettingsNodeIdPM','ngtb/resource/userSettingNewUI.jsp?userId=6fih1mq0be20001000030ag19l&amp;Action=Edit&amp;objectType=UserSetting&amp;callingMode=leftTree&amp;module=PGM','false');">
            <span >Update Settings</span></li>


    </ul>
</li>




<li >My Resources</li>



<li 
    onclick="executeNavigationAction('myResourceWorkNodeIdPM','ngtb/resource/myTeamContainerNew.jsp?objectType=ResourcesDetails&amp;selectedTabId=PLM_RDT_RESOURCE_DETAILS&amp;classId=factory_default_cl&amp;module=PLM&amp;isOldUI=true&amp;module=PGM','false');">
    <span >My Team Details</span></li>






<li >Deliverable Reviews</li>



<li 
    onclick="javascript:fnOpenPopUp('plm/search/query.jsp?pageName=Advanced Search&amp;isNew=true&amp;searchCurrentTab=Advanced Search&amp;pmSearchPopUpPage&amp;subModuleName=TechnicalReview&amp;module=PLM&amp;classId=cl_tech_review&amp;treeNode=cl_tech_review');">
    <span >Advanced Search</span></li>






<li 
    onclick="executeNavigationAction('DashboardNodeIdFotMyTR','plm/search/query.jsp?pageName=Views&amp;isNew=true&amp;searchCurrentTab=My Dashboard&amp;&amp;subModuleName=TechnicalReview&amp;module=PLM&amp;module=PGM','false');">
    <span >Dashboard</span></li>






<li >Knowledge Management</li>



<li 
    onclick="executeNavigationAction('kbTemplateIdPM','kb/kbTemplateManagement.jsp?objectType=kbTemplate&amp;module=PGM','false');">
    <span >Templates</span></li>






<li 
    onclick="executeNavigationAction('kbAddNodeIdPM','taxonomy.do?action=AddNewKBUI&amp;objectType=Document&amp;addDocFrom=HDDToKB&amp;callingMode=LeftTree&amp;IS_WIZARD_ACTION=true&amp;module=PGM','false');">
    <span >Add Knowledge</span></li>






<li 
    onclick="executeNavigationAction('kbSignOffNodeIdPDM','ngtb/KB/views/knowledgeFilterList.jsp?objectType=KbSignOamp;module=PGM','false');">
    <span >Knowledge</span></li>






<li 
    onclick="executeNavigationAction('kbRequestNodeIdPM','kb/kbRequestTab.jsp?objectType=KbRequestAcceptRejectUI&amp;module=PGM','false');">
    <span >My Requests</span></li>






<li 
    onclick="executeNavigationAction('kbSearchNodeIdPM','ngtb/knowledgeBase/knowledgeBaseContainer.jsp?objectType=simpleSearch&amp;pageAction=searchMainPage&amp;start=1&amp;isFromSearchNode=true&amp;module=PGM','false');">
    <span >Search</span></li>






<li 
    onclick="javascript:fnOpenPopUp('/search/query.jsp?pageName=Advanced Search&amp;isNew=true&amp;searchCurrentTab=Advanced Search&amp;pmSearchPopUpPage&amp;module=KB');">
    <span >Advanced</span></li>






<li 
    onclick="executeNavigationAction('boardNodeIdPM','plm/search/query.jsp?pageName=Views&amp;isNew=true&amp;searchCurrentTab=My Dashboard&amp;module=KB&amp;module=PGM','false');">
    <span >Dashboards</span></li>






<li 
    onclick="executeNavigationAction('knowledgeBaseReportsNodeIdPM','plm/report/knowledgeBaseReports.jsp?module=PGM','false');">
    <span >Reports</span></li>






<li 
    onclick="executeNavigationAction('kbConfigureAlertNodeIdPM','plm/event/ModuleEventsList.jsp?module=KB&amp;module=PGM','false');">
    <span >Alerts </span></li>






<li >Management</li>



<li ><span style="cursor: pointer; cursor: hand;"
        onclick="executeNavigationAction('extProjectRequestNodeId','plm/projectdata/projectDataFilterTabs.jsp?objectType=External Project List Master&amp;selectedTabId=EXT_PROJ_LIST_TAB_DETID&amp;classId=cl_external_project_data&amp;module=PLM&amp;module=PGM','false');">EKA
        Projects</span>




    <ul  style="display: none;top:-45px">
        <li >EKA Projects</li>


        <li 
            onclick="executeNavigationAction('extProjListPM','plm/projectdata/projectDataFilterTabs.jsp?objectType=External Project List Master&amp;selectedTabId=EXT_PROJ_LIST_TAB_DETID&amp;classId=cl_external_project_data&amp;module=PLM&amp;module=PGM','false');">
            <span >List</span></li>



        <li 
            onclick="javascript:fnOpenPopUp('plm/search/query.jsp?pageName=Advanced Search&amp;isNew=true&amp;searchCurrentTab=Advanced Search&amp;pmSearchPopUpPage=&amp;module=PGM&amp;rootSubType=External&amp;classId=cl_external_project_data&amp;subModuleName=ProjectData');">
            <span >Advanced Search</span></li>



        <li 
            onclick="executeNavigationAction('extProjDashIdPM','plm/search/query.jsp?pageName=Views&amp;isNew=true&amp;searchCurrentTab=My Dashboard&amp;subModuleName=ProjectData&amp;classId=cl_external_project_data&amp;rootSubType=External&amp;module=PGM','false');">
            <span >Dashboard</span></li>


    </ul>
</li>

CodePudding user response:

You can try this using xpath 
main_div = driver.find_elements(By.CLASS_NAME, "sub-menu")
# print(main_div)
for i in main_div:
    for span in i.find_element(By.XPATH,'.//span[@class = "leaf_line_item"]'):
        span.find_element(By.XPATH,'//li[@]//span[@]')

CodePudding user response:

the problem here is the xpath

(By.XPATH,'.//span[@class = "leaf_line_item"]')

delete the dot at the beginning of the path, it must start with a double backslash

  • Related