【问题标题】:Problems with content search in div with Selenium使用 Selenium 在 div 中进行内容搜索的问题
【发布时间】:2018-05-03 13:25:54
【问题描述】:

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

谢谢!

【问题讨论】:

  • 您能否使用 HTML 格式的文本而不是图像来更新问题以便更好地分析?
  • @DebanjanB - 我放不下完整的代码,我上传了一个片段
  • 字符串 iuijg6bzr2xs9gsueq2i 不存在于您的 HTML 中,似乎字符串 iuijg6bzr2xs9gsueq2i 是动态的。您能总结一下您尝试自动化的手动步骤吗?
  • 字符串“iuijg6bzr2xs9gsueq2i”不是动态的,它是另一个selenium为商店创建的随机名称,我要做的是搜索商店的名称并按下新的按钮会议。在完整的 HTML 中,如果您找到“iuijg6bzr2xs9gsueq2i”,请编辑代码拆分并感谢您的帮助@DebanjanB - 图片:ibb.co/b1kNv6
  • 随机是动态的。您提供的 HTML 代码中确实不存在该字符串。

标签: python python-2.7 selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

New Session 按钮上的click() 任何Strings,例如 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()

【讨论】:

  • 你的解决方案很完美,更干净
【解决方案2】:
  1. 获取所有 div 和按钮:

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

【讨论】:

  • 听起来不错,但我有:TypeError: zip argument #1 must support iteration我需要一个变量列表?
  • @jbelenus,对不起,那是我的错误。我已经更新了我的答案。再试一次。
  • 我不知道zip的使用,我已经学会了它的过程。非常感谢!
【解决方案3】:

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

driver.find_by_text('5dbnhpbwuny6rmr65h86')

find_by_text 返回一个元素的列表,所以它应该是

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

注意:未经测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2021-12-16
    • 2021-12-14
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多