Home > Blockchain >  Python Selenium navigate and click a menu voice
Python Selenium navigate and click a menu voice

Time:06-02

i have a website made of a table. Each row has two a link elements. Clicking on the second one it triggers a javascript which opens a menu.

With selenium i should navigate that menu till the "Properties" voice and to its relative submenu voice "Versions".

i tried with a simple find_element by Link_text, but it won't find the item

driver.find_element(by=By.LINK_TEXT, value="Properties").click()

i also tried with a try block but still won't find my element

try:
    myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.LINK_TEXT, "Properties")))
    print("Found")
except TimeoutException:
    print("Not Found!")

how can i access that element? I need to select the menu voice "Properties", this way will be showed a submenu with the voice "Versions" which is the one i need to click.

How to do that?

here i attach a sample of the html code of the page

<td  scope="rowgroup" nowrap="" width="100%">
    <a  href="/test/">test link</a>
    <a href="" onclick="showFunctionMenu2('rand', 7978424, event, '', '' );setSectionName('');return false"></a>
    <div  style="left: 359px; top: 516px; z-index: 5; visibility: visible;">
        <span >
            <div
                
                onclick="javascript:popup_callback( escape( '/testlink/test12' ),'');return false;"
                onm ouseover="javascript:hiLight( 'mymenu0' );doSubMenu( 'mymenu0', 'funMenu4124' );"
                onm ouseout="javascript:loLight( 'mymenu0' );"
                width="100%"
            >
                <a href="javascript:popup_callback( escape( '/testlink/test12' ),'');" onfocus="doSubMenu( 'mymenu0', 'funMenu4124' );">Download</a>
            </div>
            <div id="mymenu14"  onm ouseover="javascript:hiLight( 'mymenu14' );doSubMenu( 'mymenu14', 'funMenu4124' );" width="100%">
                <div  onclick="javascript:doKeyboardSubMenu( 'mymenu14', 'funMenu4124', event );return false;">
                    <a href="javascript:doSubMenu( 'mymenu14', 'funMenu4124' );">Properties</a>
                </div>
            </div>
            <div ><img src="/tessst/spacer.gif" alt="" width="1" height="1" /></div>
        </span>
        <span  id="mymenu14Sub">
            <div
                
                onclick="javascript:popup_callback( escape( '/testlink/viewType=1' ), '' );return false;"
                onm ouseover="javascript:hiLight( 'mymenu14.0' );doSubMenu( 'mymenu14.0', 'mymenu14Sub' );"
                onm ouseout="javascript:loLight( 'mymenu14.0' );"
                width="100%"
            >
                <a href="/testlink/viewType=1">General</a>
            </div>
            <div
                
                onclick="javascript:popup_callback( escape( '/testlink/3D1' ), '' );return false;"
                onm ouseover="javascript:hiLight( 'mymenu14.5' );doSubMenu( 'mymenu14.5', 'mymenu14Sub' );"
                onm ouseout="javascript:loLight( 'mymenu14.5' );"
                width="100%"
            >
                <a href="/testlink/func=ll&amp;objId=79">Versions</a>
            </div>
            <div ><img src="/tessst/spacer.gif" alt="" width="1" height="1" /></div>
        </span>
    </div>
</td>

CodePudding user response:

Try doing the following:

driver.find_element(By.XPATH, f"{xpath}")

In this case xpath would be the link's xpath. If your wondering how to get xpath then go right-click inspect the link element. Then the elements "source code" will come up on the right. Right-click that and click copy > copy full xpath.

CodePudding user response:

Actually, You are on the right direction but the problem is that the webdriver can't render the complex JavaScript here. So in that case, it is badly in need execute_script to render it.

myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.LINK_TEXT, "Properties")))
driver.execute_script("arguments[0].click();", myElem )
  • Related