Luck
Luck

Reputation: 23

Unable to click in a dropdown menu with Selenium

I'm trying to click on a link inside a dropdown menu, but I keep getting a TimeoutException, using XPATH.

test = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div[1]/div[2]/div/ul/li[4]/ul/li[7]/a")))
driver.execute_script("arguments[0].click();", test)

But if I try to click on the Log Out option the script sometimes works.

logOut = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div[1]/div[2]/div/ul/li[7]/ul/li/a")))
driver.execute_script("arguments[0].click();", logOut)

There are 3 dropdown menus. The test is in a menu with some 12 options and the Log Out is in another by itself. I want to understand what I'm doing wrong.

Here's the code for the menus.

<body>
    <div class="container-fluid">

        <div class="row headerLogo">
            <!--<header class="navbar-default navbar-static-top headerLogo">-->
            <div class="col-md-2">
                <div class="vericaltext">WIISCPRD23V</div>
                <a href="/Home" id="linkHome"><img src="/Content/images/Logo_menu2.png" class="logoSea" alt='SEA'  /></a>
            </div>
            <div class="col-md-10">
                <div class="row flat-nav">
                    <li class="color20 effect3">
                        <a><i class="fa fa-comments-o fa-2x"></i><span>Peq</span></a>

                        <ul class="column-based">
                                <li class="color20" style="font-weight:bold">Question</li>
                                            <li><a href="/Quest">Quest</a></li>
                                            <li><a href="/Retr">Retr</a></li>
                                <li class="color20" style="font-weight:bold">Vitss</li>
                                            <li><a href="/Vit">Vit</a></li>
                                <li class="color20" style="font-weight:bold">BC</li>
                                            <li><a href="/Trat">Trat</a></li>
                                            <li><a href="/BC">BC</a></li>
                                <li class="color20" style="font-weight:bold">CAD</li>
                                            <li><a href="/Cad">Ant</a></li>
                                <li class="color20" style="font-weight:bold">Fer</li>
                                            <li><a href="/Add">Add</a></li>
                                            <li><a href="/Emp">Emp</a></li>
                                            <li><a href="/Est">Est</a></li>
                                            <li><a href="/Seg">Seg</a></li>
                                            <li><a href="/Rec">Rec</a></li>
                                            <li><a href="/Cal">Cal</a></li>
                        </ul>
                    </li>
                    <li class="color49 effect3">
                        <a><i class="fa fa-envelope-o fa-2x"></i><span>E-mails</span></a>

            <li class="color5 effect3 divLogout">

                <a>
                    <div style="width:100%;padding-top:15px">
                        <div class="divAlinhadaEsquerda"><i class="fa fa-user-circle fa-3x"></i></div>
                        <div class="divAlinhadaEsquerda">
                            <div class="fontNomUsr">xxx</div> 
                            <div class="fontUser">xxx&nbsp;&nbsp;
                            </div>
                                <div class="fontUlti">xxx</div>
                        </div>
                    </div>
                </a>
                <ul class="column-based">
                    <li><a href="/Home/Logout"><i class="fa fa-power-off"></i><span>Sair</span></a></li>
                </ul>
            </li>
</ul>

Upvotes: 0

Views: 176

Answers (1)

SeleniumUser
SeleniumUser

Reputation: 4177

Your xpath is incorrect, please try below solution : Solution 1:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PesquisasElement=WebDriverWait(browser, 10).until(
EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Pesquisas')]")))

TratamentoElement=WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[@href='/ManterBC']")))


#Create the object for Action Chains
actions = ActionChains(driver)
actions.move_to_element(PesquisasElement).move_to_element(TratamentoElement).click()
actions.perform()

Upvotes: 1

Related Questions