使用Selenium的div中的内容搜索问题

2024-06-08 18:48:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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()

谢谢!你知道吗


Tags: divdatamanagetypebuttonactioncolcard
3条回答

任何StringsNew 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()
  1. 获取所有div和button:

    divs = driver.find_elements_by_css_selector(".o_primary") 
    buttons = driver.find_elements_by_css_selector(".btn.btn-default.oe_kanban_action.oe_kanban_action_button")
    
  2. 浏览div元素列表并找到所需的元素,然后在相应的按钮上执行单击操作:

    for div, button in zip(divs, buttons):
    
      if div.text == "5dbnhpbwuny6rmr65h86":
    
        button.click()
    

你听说过Splinter吗?它是Selenium之上的一个抽象层,允许您通过文本来查找元素:https://splinter.readthedocs.io/en/latest/finding.html

driver.find_by_text('5dbnhpbwuny6rmr65h86')

find\u by \u text返回元素列表,因此应该是

element = driver.find_by_text('5dbnhpbwuny6rmr65h86').first
element.find_by_xpath("//button[contains(text() , 'New Session')]").first.click()

注:未测试

相关问题 更多 >