问题点击<a> tag in functional test with selenium

时间:2016-01-18 15:31:47

标签: python django selenium

I am to make a functional test and I need to make click on <a> tag, but I try some ways and really I don't know, I try with the commands in this url https://saucelabs.com/resources/articles/the-selenium-2018click2019-command,没有任何效果。

import time
from selenium import webdriver
from unittest import TestCase

#driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
class GoToLogin(TestCase):

    driver = webdriver.Firefox()
    driver.get('http://192.168.56.101:8000/login/');
    #time.sleep(5) # Let the user actually see something!
    user_field = driver.find_element_by_id('id_username')
    user_field.send_keys('lruedc')

    password_field = driver.find_element_by_id('id_password')
    password_field.send_keys('lejoruca123')

    button_field = driver.find_element_by_id('btnlogin')
    button_field.click()

    #time.sleep(5) # Let the user actually see something!
    user_field.submit()
    self.driver.quit()

html文件内容:

<div id="menuform">
    <div style="width: 270px;">
         <ul class="menu_ulli">
           <li class="menu_ulli"  name="Procesos">
             <a data-toggle="dropdown" href="#">Procesos</a>
             <ul class="menu_ulli">
               <li class="menu_ulli" id="Prestar Servicios" name="Prestar Servicios"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Prestar Servicios</a>
                   <ul class="menu_ulli">
                       <li class="menu_ulli" name="Gestionar Servicios BPO">
                           <a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Gestionar Servicios BPO</a>
                           <ul class="menu_ulli">
                               <li class="menu_ulli" name="Generar la Estrategía del Servicio"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Generar la Estrategía del Servicio</a>
                                   <ul class="menu_ulli">
                                        <li class="menu_ulli" name="Gestiornar vertical-Servicio al cliente"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Gestiornar vertical-Servicio al cliente</a>
                                         <ul class="menu_ulli">
                                            <li class="menu_ulli" name="Analizar línea de Servicio"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Analizar línea de Servicio</a></li>
                                            <li class="menu_ulli" name="Diseñar la estrategia"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Diseñar la estrategia</a></li>
                                            <li class="menu_ulli" name="Comunicar la estrategia"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Comunicar la estrategia</a></li>
                                         </ul>
                                        </li>
                                        <li class="menu_ulli" name="Gestiornar vertical-Ventas Telefónicas"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Gestiornar vertical-Ventas Telefónicas</a></li>
                                        <li class="menu_ulli" name="Gestiornar vertical-Cobranzas"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Gestiornar vertical-Cobranzas</a></li>
                                        <li class="menu_ulli" name="Gestiornar vertical-Back Office"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Gestiornar vertical-Back Office</a></li>
                                   </ul>
                               </li>
                               <li class="menu_ulli" name="Gestionar Acuerdos y Cambios en la Operación"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Gestionar Acuerdos y Cambios en la Operación</a></li>
                               <li class="menu_ulli" name="Pronosticar, planificar y programar recursos"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">Pronosticar, planificar y programar recursos</a></li>
                           </ul>
                       </li>
                   </ul>
               </li>                
             </ul>
           </li>
        </ul>
        <ul class="menu_ulli">
           <li class="menu_ulli" name="Estrategia">
             <a data-toggle="dropdown" href="#">Estrategia</a>
             <ul class="menu_ulli">
               <li class="menu_ulli" name="Corporativo"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">Corporativo</a>
                   <ul class="menu_ulli">
                       <li class="menu_ulli" name="Corporativo Emtelco"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">Corporativo Emtelco</a>
                       <ul class="menu_ulli">
                           <li class="menu_ulli" id="1" name="BSC 2013-2014"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">BSC 2013-2014</a></li>
                           <li class="menu_ulli" id="2" name="BSC 2014-2015"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">BSC 2014-2015</a></li>
                       </ul>
                       </li>
                   </ul>
               </li>
               <li class="menu_ulli" name="UEN"><a href="{% url 'key_process_indicator_bsc:process_indicator' %}">UEN</a>
                   <ul class="menu_ulli">
                       <li class="menu_ulli" name="Contact Center"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">Contact Center</a>
                           <ul class="menu_ulli">
                               <li class="menu_ulli" id="3" name="BSC 2013-2014"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">BSC 2013-2014</a></li>
                               <li class="menu_ulli" id="4" name="BSC 2014-2015"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">BSC 2014-2015</a></li>
                           </ul>
                       </li>
                       <li class="menu_ulli" name="Premisas"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">Premisas</a>
                           <ul class="menu_ulli">
                               <li class="menu_ulli" id="5" name="BSC 2013-2014"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">BSC 2013-2014</a></li>
                               <li class="menu_ulli" id="6" name="BSC 2014-2015"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">BSC 2014-2015</a></li>
                           </ul>
                       </li>
                       <li class="menu_ulli" name="Servicios Complementarios"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">Servicios Complementarios</a>
                           <ul class="menu_ulli">
                               <li class="menu_ulli" id="7" name="BSC 2013-2014"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">BSC 2013-2014</a></li>
                               <li class="menu_ulli" id="8" name="BSC 2014-2015"><a href="{% url 'key_process_indicator_bsc:bsc_indicator' %}">BSC 2014-2015</a></li>
                           </ul>
                       </li>
                   </ul>
               </li>                  
             </ul>
           </li>
        </ul>
    </div>
</div>

我需要点击<a>标记。

1 个答案:

答案 0 :(得分:0)

点击您可以在评论中提及的任何httpCon.getHeaderField("errorMessage") 标记

<a>

这将点击html中的第一个driver.find_element_by_tag_name("a").click() (如果可点击)。

但是,建议不要单击标签名称找到的元素,因为通常有许多相同类型的标签。最好使用other methods来定位元素。