【问题标题】:How can I insert a button tag into html using Selenium Java?如何使用 Selenium Java 将按钮标签插入 html?
【发布时间】:2021-10-24 20:34:12
【问题描述】:

是否可以插入按钮元素

(<button type= "submit">)

使用driver.execute_script方法进入页面的html代码? 以及如何指定它的位置?

比如有一个文本字段email(driver.find_element_by_xpath ("//input[@name= 'email']"),我需要在这个字段下面或者这个字段上面插入提交按钮并点击它。

【问题讨论】:

    标签: javascript python selenium selenium-webdriver automated-tests


    【解决方案1】:

    Selenium 不支持此类操作。
    Selenium 旨在执行真实用户可以使用 GUI 执行的操作。
    作为用户,我无法将元素添加到 HTML 网页中。
    我可以点击元素、获取元素文本、滚动页面等。

    【讨论】:

      【解决方案2】:

      基本上Selenium 用于测试。因此,根据我的建议,无需从 Selenium 添加任何内容。

      如果没有submit按钮,那肯定是你页面的bug。

      因此,我们建议您再次检查您的页面。

      Selenium website link ? https://www.selenium.dev/

      【讨论】:

      • 网站有 bug ?
      【解决方案3】:

      直接的答案是

      但是通过 js 你可以更新 Selenium 中的一个属性。见下文。

      <a href="foo.html" id="foo-id" class="foo-class"> foo-text </a>
      

      您基本上可以将此 foo-text 更新为任何其他字符串,例如:

      driver.execute_script("document.getElementById('foo-id').innerHTML = '201';");
      

      另外,如果您看到我们有 id 作为 attribute 的值 foo-id,如果您想将该 id 更新为 someotherfoo-id,您可以执行以下操作:-

      element =  driver.find_element_by_class_name("foo-class");
      driver.execute_script("arguments[0].setAttribute('id', 'someotherfoo-id')", element);
      

      执行此操作后,您必须看到 UI 的变化。所以基本上你可以像这样进行修改。

      【讨论】:

        猜你喜欢
        • 2017-10-31
        • 1970-01-01
        • 2013-07-21
        • 1970-01-01
        • 1970-01-01
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多