gpt4 book ai didi

python - 使用 Selenium 在 div 中进行内容搜索的问题

转载 作者:行者123 更新时间:2023-12-01 02:26:07 25 4
gpt4 key购买 nike

我在使用 Selenium 按下此类按钮时遇到问题,因为我查找“5dbnhpbwuny6rmr65h86”的名称和按钮在 Python 中位于不同的 div 中。

完整的 HTML 代码:https://codeshare.io/a39b3g .

Image HTML .

HTML 代码示例:

<div class="o_kanban_view o_kanban_dashboard o_pos_kanban o_cannot_create o_kanban_ungrouped" style="display: flex;"><div class="o_kanban_record">
<div class="o_kanban_card_header">
<div class="o_kanban_card_header_title">
<div class="o_primary">5dbnhpbwuny6rmr65h86</div>
<div class="o_secondary">Unused</div>
</div>
<div class="o_kanban_manage_button_section">
<a class="o_kanban_manage_toggle_button" href="#">Más <i class="fa fa-caret-down"></i></a>
</div>
</div>
<div class="container o_kanban_card_content o_visible">
<div class="row">
<div class="col-xs-6 o_kanban_primary_left">






<button class="btn btn-default oe_kanban_action oe_kanban_action_button" data-name="open_session_cb" data-type="object" type="button">New Session
</button>
</div>
<div class="col-xs-6 o_kanban_primary_right">




</div>
</div>
</div><div class="container o_kanban_card_manage_pane o_invisible">
<div class="row">
<div class="col-xs-6 o_kanban_card_manage_section o_kanban_manage_view">
<div class="o_kanban_card_manage_title">
<span>Ver</span>
</div>
<div>
<a data-name="341" data-type="action" href="#" class=" oe_kanban_action oe_kanban_action_a">Sesiones</a>
</div>
<div>
<a data-name="342" data-type="action" href="#" class=" oe_kanban_action oe_kanban_action_a">Pedidos de ventas</a>
</div>
</div>
<div class="col-xs-6 o_kanban_card_manage_section o_kanban_manage_new">
<div class="o_kanban_card_manage_title">
<span>Informes</span>
</div>
<div>
<a data-name="343" data-type="action" href="#" class=" oe_kanban_action oe_kanban_action_a">Pedidos</a>
</div>
</div>
</div>

<div class="o_kanban_card_manage_settings row">
<div class="col-xs-12 text-right">
<a data-type="edit" href="#" class=" oe_kanban_action oe_kanban_action_a">Configuración</a>
</div>
</div>
</div>
</div><div class="o_kanban_record o_kanban_ghost"></div><div class="o_kanban_record o_kanban_ghost"></div><div class="o_kanban_record o_kanban_ghost"></div><div class="o_kanban_record o_kanban_ghost"></div><div class="o_kanban_record o_kanban_ghost"></div><div class="o_kanban_record o_kanban_ghost"></div></div>

我想出了类似的方法,但我没有正确的解决方案:

for div in driver.find_elements_by_xpath("//div[@class='o_kanban_record']"):

if div.find_elements_by_xpath("//div[contains(text() , '5dbnhpbwuny6rmr65h86')]") != []:

div.find_elements_by_xpath("//button[contains(text() , 'New Session')]").click()

谢谢!

最佳答案

对于任何字符串,例如,在New Session按钮上click()iuijg6bzr2xs9gsueq2i5dbnhpbwuny6rmr65h86,您可以借助函数并传递任何String来获取相关的New Session按钮点击。

最终的解决方案,检测按钮的状态是:

driver.find_elements_by_xpath("//div[@class='o_primary' and contains(text(), '%s')]/parent::div[*]/parent::div[*]/parent::div[*]/descendant::button[@data-name='open_session_cb']" % (shop))[0].click()

或者

driver.find_elements_by_xpath("//div[@class='o_primary' and contains(text(), '%s')]/parent::div[*]/parent::div[*]/parent::div[*]/descendant::button[@data-name='open_ui']" % (shop))[0].click()

关于python - 使用 Selenium 在 div 中进行内容搜索的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47390107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com