【问题标题】:how to find xpath in selenium webdriver when button webelement is dynamic当按钮webelement是动态的时如何在selenium webdriver中找到xpath
【发布时间】:2015-09-14 09:26:59
【问题描述】:

以下是页面的 html,我需要获取将在 selenium webdriver 中工作的按钮的 xpath。或者有什么方法可以点击 selenium webdriver 中的按钮 如果它在java中会很棒,否则python和其他语言也会受到赞赏。

<button style="" onclick="saveAndContinueEdit('http://192.168.1.99/cue_extensions/magento1.9/index.php/admin/catalog_product/save/back/edit/tab/{{tab_id}}/id/899/key/6ed7b01b3a2136d6ff896d9380e281d5/')" class="scalable save" type="button" title="Save and Continue Edit" id="**id_636f54090db18b58eecba78e9e6aa6b7**">
  <span><span><span>Save and Continue Edit</span></span></span>
</button>

但是当我下次登录时,html 更改为

<button style="" onclick="saveAndContinueEdit('http://192.168.1.99/cue_extensions/magento1.9/index.php/admin/catalog_product/save/back/edit/tab/{{tab_id}}/id/903/key/29d8597a8ec8f63d9b09addae444d4c0/')" class="scalable save" type="button" title="Save and Continue Edit" id="id_c2fe56a74ef2a166dcf0ae4ed7f8e391">
  <span><span><span>Save and Continue Edit</span></span></span>
</button>[![enter image description here][1]][1]

【问题讨论】:

    标签: selenium button xpath selenium-webdriver


    【解决方案1】:

    在 Java 中:

    driver.findElement(By.xpath("//button[@title='Save and Continue Edit']")).click();
    

    在python中

    driver.find_elements_by_xpath("//button[@title='Save and Continue Edit']").click()
    

    【讨论】:

      【解决方案2】:

      如果您可以假设标题始终为“保存并继续编辑”(即 Magento 后端语言未更改),请使用

      //button[@title='Save and Continue Edit']
      

      如前所述。一种更稳健的方法是通过 onclick 属性进行搜索,例如:

      //button[starts-with(@onclick,"saveAndContinueEdit(")]
      

      【讨论】:

        【解决方案3】:

        试试以下方法:

        在 Java 中:

        driver.findElement(By.Xpath("//span[contains(text(), 'Save and Continue Edit')]")).click();
        

        在 Python 中:

        driver.find_element_by_xpath("//span[contains(text(), 'Save and Continue Edit')]").click()
        

        【讨论】:

          【解决方案4】:

          你可以使用下面的xpath——

          //span[@title="Save and Continue Edit"]
          

          【讨论】:

          • 不,你不能,那是非法的 XPath。也许您的意思是//*[@..]//span[@...]? `\` 后面必须始终跟一个 NodeTest。
          【解决方案5】:

          在java中你会发现使用以下命令:

          driver.findElement(By.Xpath("//button[contains(.,'Save and Continue Edit')]")).click();

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-02-04
            • 1970-01-01
            • 1970-01-01
            • 2018-12-01
            相关资源
            最近更新 更多