¿Cómo presionar / hacer clic en el botón usando Selenium si el botón no tiene el Id?


Tengo 2 botones Cancelar y botón Siguiente en la misma página, pero solo tiene un id (ver el siguiente código). Quería presionar Siguiente, pero cada vez que está identificando el botón cancelar solo no el botón Siguiente. ¿Cómo resolver este problema?

<td align="center">
     <input type="button" id="cancelButton" value="Cancel" title="cancel" class="Submit_Button" style="background-color: rgb(0, 0, 160);">
     <input type="submit" value="Next" title="next" class="Submit_Button">
</td>
Author: ChanGan, 2012-01-15

7 answers

En Selenium IDE se puede hacer:

Command   |   clickAndWait
Target    |   //input[@value='Next' and @title='next']

Debería funcionar bien.

 16
Author: faramka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-16 13:15:41

Use selector xpath (aquí está el tutorial rápido ) en lugar de id:

#python:
from selenium.webdriver import Firefox

YOUR_PAGE_URL = 'http://mypage.com/'
NEXT_BUTTON_XPATH = '//input[@type="submit" and @title="next"]'

browser = Firefox()
browser.get(YOUR_PAGE_URL)

button = browser.find_element_by_xpath(NEXT_BUTTON_XPATH)
button.click()

O, si usa Selenium "vanilla", simplemente use el mismo selector xpath en lugar del id del botón:

NEXT_BUTTON_XPATH = '//input[@type="submit" and @title="next"]'
selenium.click(NEXT_BUTTON_XPATH)
 18
Author: Misha Akovantsev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-19 16:53:30

Utilice los atributos text y value en lugar del id

driver.findElementByXpath("//input[@value='cancel'][@title='cancel']").click();

Del mismo modo para el Siguiente.

 4
Author: user1710861,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-16 05:44:30

Para el botón Siguiente puede usar xpath o cssSelector de la siguiente manera:

Xpath para el botón Siguiente: / / input [@value = 'Next']

CssPath para el botón Siguiente: input [value = Next]

 2
Author: Ripon Al Wasim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-16 04:52:52

No es necesario utilizar solo identificador como localizadores de elementos. Puedes usar algunas maneras de encontrar un elemento. Lee este artículo y elige el mejor para ti.

 1
Author: faramka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-16 08:14:53

Puede usar xpath for para identificar ese elemento.

 0
Author: Rohit Ware,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-16 11:33:15
    You can achieve this by using cssSelector 
    // Use of List web elements:
    String cssSelectorOfLoginButton="input[type='button'][id='login']"; 
    //****Add cssSelector of your 1st webelement
    //List<WebElement> button 
    =driver.findElements(By.cssSelector(cssSelectorOfLoginButton));
    button.get(0).click();

    I hope this work for you
 0
Author: mkumar0304,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-01 12:54:18