2016-01-18 62 views
-2

我要做一個功能測試,我需要點擊<a>標籤,但我嘗試了一些方法,真的我不知道,我試着用命令在這個URL https://saucelabs.com/resources/articles/the-selenium-2018click2019-command,沒有任何工作,如果有人可以幫助我,我會apreciate它。謝謝。問題點擊<a>標籤功能測試

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>標籤

+0

你能分享這個html嗎? – Guy

+0

當然,這是html文件 –

+0

如果您嘗試添加文件,它不起作用。如果將其添加到問題中會更好。 – Guy

回答

0

要在任何<a>標籤點擊你提到的評論,你可以做

driver.find_element_by_tag_name("a").click() 

這將點擊第一個<a>在html中(如果它是可點擊的)。

但是,不推薦單擊通過標籤名稱找到的元素,因爲通常會有許多相同類型的標籤。最好使用other methods來定位元素。