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

发布于 2025-01-10 16:09:35 字数 4979 浏览 0 评论 0原文

我需要单击导出器。

这是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>

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

I need to click on Exportar.

This is the html code

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

Thank you for the help. This is just a small part of the code.
Thank you for the help. This is just a small part of the code.
Thank you for the help. This is just a small part of the code.
Thank you for the help. This is just a small part of the code.
Thank you for the help. This is just a small part of the code.
Thank you for the help. This is just a small part of the code.
Thank you for the help. This is just a small part of the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界和平 2025-01-17 16:09:36

要定位文本为 Exportar 的元素,您可以使用以下任一定位器策略

  • < p>css_selector

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

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

To locate the element with text as Exportar you can use either of the following locator strategies:

  • css_selector:

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

    //button[@class='call-to-action ui-button ui-corner-all ui-widget button-default' and text()='Exportar']
    
—━☆沉默づ 2025-01-17 16:09:36

试试这个:

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

Try this :

driver.find_element_by_xpath('//button[@class="call-to-action ui-button ui-corner-all ui-widget button-default"]').click()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文