Selenium:如何使用 FindElementByXPath 来识别元素

Selenium: How to use FindElementByXPath to identify the element

我需要点击 Exportar。

这是html代码

<div class="ui-dialog-buttonset">
  <button type="button" title="" style="" class="call-to-action ui-button ui-corner-all ui-widget button-default">Exportar</button>
  <button type="button" title="" class="ui-button ui-corner-all ui-widget button-cancel">Cancelar</button>
</div>
<div tabindex="-1" role="dialog" style="height: auto; width: 440px; top: 211.656px; left: 460px; z-index: 101;" class="ui-dialog ui-widget ui-widget-content ui-front ui-dialog-buttons ui-draggable ui-dialog-newest" aria-describedby="popup-exportar-relatorio" aria-labelledby="ui-id-2"><div class="ui-dialog-titlebar ui-corner-all ui-widget-header ui-helper-clearfix ui-draggable-handle"><span id="ui-id-2" class="ui-dialog-title">Exportar relatório</span><button type="button" class="ui-button ui-corner-all ui-widget ui-button-icon-only ui-dialog-titlebar-close" title=""><span class="ui-button-icon ui-icon ui-icon-closethick"></span><span class="ui-button-icon-space"> </span></button></div><div id="popup-exportar-relatorio" style="text-align: left; width: auto; min-height: 0px; max-height: none; height: auto;" class="ui-dialog-content ui-widget-content">
<div class="col-xs-12 group-item-form">
<label for="formato" class="label-item-form">Formato de arquivo</label>
<select id="formato" name="formato" class="wh100 bling-item-form">
<option selected="" value="csv">CSV</option>
<option value="pdf">PDF</option>
<option value="xls">XLS</option>
</select>
</div>
<div id="div-orientacao-pagina" class="col-xs-12 group-item-form" style="display: none;">
<span class="linha_form wh100 margin_top12"></span>
<label for="orientacao-pagina-select" class="label-item-form">Orientação da página</label>
<select id="orientacao-pagina-select" name="orientacao-pagina-select" class="wh100 bling-item-form">
<option selected="" value="retrato">Retrato</option>
<option value="paisagem">Paisagem</option>
</select>
</div>
<div id="div-exportar-relatorio-opcoes" class="col-xs-12 group-item-form" style="display: none;">
<span class="linha_form wh100 margin_top12"></span>
<label for="orientacao-pagina-select" class="label-item-form">Orientação da página</label>
<select id="orientacao-pagina-select" name="orientacao-pagina-select" class="wh100 bling-item-form">
<option selected="" value="retrato">Retrato</option>
<option value="paisagem">Paisagem</option>
</select>
</div>
<div id="options-container">
<div id="div_exportar_cabecalho" class="col-xs-12 group-item-form" style="display: none;">
<span class="linha_form wh100 margin_top12"></span>
<div class="vertical-align-inline"><div class="input-checkbox">
<input type="checkbox" id="opc_exportar_cabecalho" name="opc-exportar-cabecalho" data-option="cabecalho" checked="">

<label for="opc_exportar_cabecalho"></label></div><label for="opc_exportar_cabecalho" class="label-item-form-input">Exportar com logo da empresa</label></div>
</div>

<h5 class="subtitulo-popup margin_top10" style="padding: 5px">Opções</h5>
<div class="form-item wh100">
<div class="vertical-align-inline"><div class="input-checkbox">
<input type="checkbox" id="opc-exportar-totalizador" name="opc-exportar-totalizador" data-option="totalizador">

<label for="opc-exportar-totalizador"></label></div><label for="opc-exportar-totalizador" class="label-item-form-input">
Exportar com totalizadores
</label></div>
</div>
<br>
</div>
<div class="col-xs-12 group-item-form">
<div id="xls-cell-limit-info" class="alert-mini-box alert-mini-box-info alert-mini-box-transparent" style="display: none;">
<p>
O formato <strong>XLS</strong> possui limite de <strong>trinta mil caracteres</strong> por célula de acordo com sua especificação.<br>
Recomenda-se o uso do formato <strong>CSV</strong> para exportação de dados que excedam este limite.
</p>
</div>
</div>
</div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" title="" style="" class="call-to-action ui-button ui-corner-all ui-widget button-default">Exportar</button><button type="button" title="" class="ui-button ui-corner-all ui-widget button-cancel">Cancelar</button></div></div></div>

感谢您的帮助。这只是代码的一小部分。 感谢您的帮助。这只是代码的一小部分。 感谢您的帮助。这只是代码的一小部分。 感谢您的帮助。这只是代码的一小部分。 感谢您的帮助。这只是代码的一小部分。 感谢您的帮助。这只是代码的一小部分。 感谢您的帮助。这只是一小部分代码。

试试这个:

driver.find_element_by_xpath('//button[@class="call-to-action ui-button ui-corner-all ui-widget button-default"]').click()

要定位文本为 Exportar 的元素,您可以使用以下任一方法 :

  • :

    div.ui-dialog-buttonset > button.call-to-action.ui-button.ui-corner-all.ui-widget.button-default
    
  • :

    //button[@class='call-to-action ui-button ui-corner-all ui-widget button-default' and text()='Exportar']