How to access an object per class?

1

This is my code fragment that generates me error.

boton = browser.find_element_by_css_selector(".boton boton-rojo")
boton[0].click()

Here is the html object:

<div class="boton boton-rojo"><a href="https://******.*****.com.co/iModelWeb/vista/cotizar/cotizar.jsf?&amp;nit=9007123621&amp;nombreusuario=******&amp;agente=****&amp;cod_agt=*****&amp;agentev=98772&amp;usuario=******&amp;cod_cia=&amp;cia=&amp;token=******.BFE96A5FCF41773D22A8D88E227160DD&amp;idPerfil=EI&amp;tokenNew=TECAASES.BFE96A5FCF41773D22A8D88E227160DDBFE96A5FCF41773D22A8D88E227160DD&amp;nombBreCrumOfiVirt=Cotizar%20-%20Emitir" onclick="cmdRegistrarNavegacion([{name:'paramId', value:'OPT_ATG_AUTOGES_II_COTIZAR_HOME'},{name:'paramNombre', value:'Cotizar - Emitir'},{name:'paramTipo', value:'W'},{name:'paramUrl', value:'https://cotiza.mapfre.com.co/iModelWeb/vista/cotizar/cotizar.jsf?&amp;nit=9007123621&amp;nombreusuario=9007123621&amp;agente=98772&amp;cod_agt=98772&amp;agentev=98772&amp;usuario=TECAASES&amp;cod_cia=&amp;cia=&amp;token=TECAASES.BFE96A5FCF41773D22A8D88E227160DD&amp;idPerfil=EI&amp;tokenNew=TECAASES.BFE96A5FCF41773D22A8D88E227160DDBFE96A5FCF41773D22A8D88E227160DD&amp;nombBreCrumOfiVirt=Cotizar - Emitir'}]);" target="_blank">
                            <img src="../../resources/images/btn-cotizadores.png"></a>
                    </div>

This is the error that generates me:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Una
ble to locate element: {"method":"css selector","selector":".boton boton-rojo"}
  (Session info: chrome=63.0.3239.84)
  (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87a
f1),platform=Windows NT 6.1.7601 SP1 x86_64)

You need to click the first object of that class, in the html there are only two. I use the chrome driver can someone help me?

    
asked by Diego Lopez 26.12.2017 в 17:49
source

2 answers

1

You can use find_elements_by_class_name to find the elements in your document through your class but you will only pass the class boton-rojo , be careful with elements plural since there is also an option find_element_by_class_name singular to obtain a single element.

DOM

<div class="boton boton-rojo">Bton1</div>

<div class="boton boton-rojo">Bton 2</div>

Python

boton = browser.find_elements_by_class_name("boton-rojo")
print boton[0].text;
print boton[1].text;

boton[0].click()
    
answered by 26.12.2017 / 18:16
source
0

You could try something like this:

<html>
 <body>
  <p class="content">Site content goes here.</p>
</body>
<html>

To access the item p with class content :

content = driver.find_element_by_class_name('content')

Here you can find more info on find_element_by_class_name

    
answered by 26.12.2017 в 18:01