Search code examples
pythonselenium-webdriverselenium-chromedriver

Find element by XPATH dont work Selenium/Python


I am trying to select a element with python selenium but the XPATH has a different format and I do not know how to select it.

This is the HTML code of the element, which the image of what it represents it's below

<span class="type-multiple_choice field-actual question-width state-mandatory" ng-switch="::field.type" ng-class="{'state-mandatory': field.mandatory, 'state-readonly': field.read_only, 'state-hidden': field.hidden, 'has-error': field.isInvalid}"><!-- ngSwitchWhen: boolean --><!-- ngIf: ::field.details --><!-- ngSwitchWhen: choice --><!-- ngSwitchWhen: boolean_confirm --><!-- ngSwitchWhen: color --><!-- ngSwitchWhen: css --><!-- ngSwitchWhen: currency2|document_id|domain_id|schedule_date_time|integer_date --><!-- ngSwitchWhen: glide_duration --><!-- ngSwitchWhen: url --><!-- ngSwitchWhen: field_name --><!-- ngSwitchWhen: field_list --><!-- ngSwitchWhen: glide_date --><!-- ngSwitchWhen: glide_date_time --><!-- ngSwitchWhen: glide_list --><!-- ngSwitchWhen: glyphicon --><!-- ngSwitchWhen: xml --><!-- ngSwitchWhen: html_template --><!-- ngSwitchWhen: json --><!-- ngSwitchWhen: masked --><!-- ngSwitchWhen: multiple_choice --><sp-radio-option ng-switch-when="multiple_choice" glide-form="getGlideForm()" field="field" cat-item-sys-id="formModel.sys_id" class="ng-scope ng-isolate-scope"><!-- ngInclude: --><ng-include src="getTemplateUrl()" class="ng-scope"><fieldset id="sp_formfield_u_rd_tipo_do_servi_o_solicitado" aria-labelledby="sp_radio_down_label_u_rd_tipo_do_servi_o_solicitado" class="radio m-t-none ng-scope"><legend class="ng-hide ng-binding" id="sp_radio_down_label_u_rd_tipo_do_servi_o_solicitado"><span ng-show="field.isMandatory() &amp;&amp; !field.mandatory_filled()" aria-hidden="true" class="ng-hide" style="">Obrigatório -</span><span ng-show="field.isMandatory() &amp;&amp; field.mandatory_filled()" aria-hidden="false" class="" style="">Obrigatório preenchido -</span>Tipo do Serviço Solicitado:</legend><div role="radiogroup" class="radio-control"><!-- ngRepeat: c in field.choices --><div ng-repeat="c in field.choices" class="block ng-scope"><label class="radio-element"><input aria-checked="false" ng-value="c.value" ng-model="fieldValue" ng-model-options="{getterSetter: true}" ng-disabled="field.isReadonly()" ng-init="onButtonLoad()" name="radio_button_down_423a4c351bb0b0501360844fe54bcbe9_24b9e7611b30b0501360844fe54bcb39" type="radio" class="ng-valid ng-not-empty ng-dirty ng-touched" role="radio" value="RDCO00006" tabindex="-1" style=""><span class="ng-binding">Reclassificação no Período<!-- ngIf: c.priceLabel && field.value != c.value --></span></label></div><!-- end ngRepeat: c in field.choices --><div ng-repeat="c in field.choices" class="block ng-scope"><label class="radio-element"><input aria-checked="false" ng-value="c.value" ng-model="fieldValue" ng-model-options="{getterSetter: true}" ng-disabled="field.isReadonly()" ng-init="onButtonLoad()" name="radio_button_down_423a4c351bb0b0501360844fe54bcbe9_24b9e7611b30b0501360844fe54bcb39" type="radio" class="ng-valid ng-not-empty ng-dirty ng-touched" role="radio" value="RDCO00007" tabindex="-1" style=""><span class="ng-binding">Reclassificação fora do Período<!-- ngIf: c.priceLabel && field.value != c.value --></span></label></div><!-- end ngRepeat: c in field.choices --><div ng-repeat="c in field.choices" class="block ng-scope"><label class="radio-element"><input aria-checked="true" ng-value="c.value" ng-model="fieldValue" ng-model-options="{getterSetter: true}" ng-disabled="field.isReadonly()" ng-init="onButtonLoad()" name="radio_button_down_423a4c351bb0b0501360844fe54bcbe9_24b9e7611b30b0501360844fe54bcb39" type="radio" class="ng-valid ng-not-empty ng-dirty ng-touched ng-valid-parse" role="radio" value="RDCO00008" tabindex="0" style=""><span class="ng-binding">Rateio de Despesas<!-- ngIf: c.priceLabel && field.value != c.value --></span></label></div><!-- end ngRepeat: c in field.choices --></div></fieldset></ng-include></sp-radio-option><!-- end ngSwitchWhen: --><!-- ngSwitchWhen: numericscale --><!-- ngSwitchWhen: multi_two_lines --><!-- ngSwitchWhen: multi_small --><!-- ngSwitchWhen: price --><!-- ngSwitchWhen: currency --><!-- ngSwitchWhen: password --><!-- ngSwitchWhen: password2 --><!-- ngSwitchWhen: properties --><!-- ngSwitchWhen: reference --><!-- ngSwitchWhen: script_server --><!-- ngSwitchWhen: script --><!-- ngSwitchWhen: table_name --><!-- ngSwitchWhen: textarea --><!-- ngSwitchWhen: html --><!-- ngSwitchWhen: translated_html --><!-- ngSwitchWhen: user_image --><!-- ngSwitchWhen: widget --><!-- ngSwitchWhen: widget_value --><!-- ngSwitchWhen: integer --><!-- ngSwitchWhen: decimal|float --><!-- ngSwitchWhen: sc_multi_row --><!-- ngSwitchWhen: email --><!-- ngSwitchWhen: rich_text_label --><!-- ngSwitchWhen: sc_attachment --><!-- ngSwitchDefault: true --></span>


enter code here

enter image description here

I had tried to select by XPATH as this:

browser.find_element(By.XPATH,'//*@id="sp_formfield_u_rd_tipo_do_servi_o_solicitado"]/div/div[3]/label/input').click()

I also had tried by class and Value, but anything works


Solution

  • Use Below xpath. You have to use xpath with respect to text.

    //span[text()='Rateio de Despesas']/..
    
    browser.find_element(By.XPATH, '//span[text()='Rateio de Despesas']/..').click()