【问题标题】:Trying to apply a selenium test on bootstrap modal using python尝试使用 python 在引导模式上应用硒测试
【发布时间】:2022-01-15 14:10:58
【问题描述】:

这是 HTML:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<div class="modal-content">
            <div class="modal-header">
                <h5 id="add-title" class="modal-title">Add a text</h5>
                <button type="button" class="close" data-dismiss="modal">×</button>
            </div>
            <section id="add-popup-card-body">
                <form id="add-form"><div class="sm-form modal-body">
<h6 id="add-popup-card-body-subtitle" class="card-subtitle mb-2 text-muted">Please enter text</h6>
<input type="text" id="add-input-form" class="sm-form modal-body form-control validate" pattern="\S+" style="text-transform:uppercase" maxlength="32" placeholder="tag" onkeyup="this.value = this.value.toUpperCase();" required="">
<div><small>*Spaces are not allowed</small></div>
</div>`enter code here`
<div class="modal-footer justify-content-center">
<input type="submit" class="btn btn-primary">
</div></form>
            </section>
        </div>
</body>
</html>

我所需要的只是找到一种方法让 Selenium 测试关闭模式。到目前为止,我已经尝试过这些方法,但都没有奏效:

self.driver.findElement(By.className("close")).click()
self.driver.findElement(By.xpath("//button[@class = 'close']")).click()
self.driver.find_element(By.CSS_SELECTOR, 'button[class="close"]').click()

【问题讨论】:

  • 出现什么url和错误?
  • ..\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py:247: ElementNotInteractableException
  • @David : 查看下面给出的解决方案,让我知道状态。

标签: python html selenium testing


【解决方案1】:

在 Selenium 中基本上有 4 种点击方式。

我将使用这个 xpath

//button[@class='close' and @data-dismiss='modal' and text()='×']

代码试用 1:

time.sleep(5)
driver.find_element_by_xpath("//button[@class='close' and @data-dismiss='modal' and text()='×']").click()

代码试用 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='close' and @data-dismiss='modal' and text()='×']"))).click()

代码试用 3:

time.sleep(5)
button = driver.find_element_by_xpath("//button[@class='close' and @data-dismiss='modal' and text()='×']")
driver.execute_script("arguments[0].click();", button)

代码试用 4:

time.sleep(5)
button = driver.find_element_by_xpath("//button[@class='close' and @data-dismiss='modal' and text()='×']")
ActionChains(driver).move_to_element(button).click().perform()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PS:如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

检查步骤:

Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到突出显示 987654334@匹配节点。

【讨论】:

    【解决方案2】:

    要点击元素,您可以使用以下任一Locator Strategies:

    • 使用css_selector:

      driver.find_element(By.CSS_SELECTOR, "button.close[data-dismiss='modal']").click()
      
    • 使用xpath:

      driver.find_element(By.XPATH, "//button[@class='close' and @data-dismiss='modal'][text()='×']").click()
      

    理想情况下,由于所需的元素是 Modal Dialog Box 以单击 可点击 元素,因此您需要为 element_to_be_clickable() 诱导 WebDriverWait,您可以使用以下任一Locator Strategies:

    • 使用CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.close[data-dismiss='modal']"))).click()
      
    • 使用XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='close' and @data-dismiss='modal'][text()='×']"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

      【解决方案3】:

      感谢您的帮助。我已经尝试了上述所有解决方案,但都没有奏效。我遇到了类似的错误。经过一番阅读后,我调用了一个高于“modal-content”的 id 并添加了 3 个 div 。通过使用此代码:

      self.driver.find_element_by_xpath("//*[@id='add-tag popup']/div/div/div/button").click()
      

      【讨论】:

        猜你喜欢
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 2020-05-11
        • 2021-05-27
        • 2011-12-05
        • 2021-11-30
        相关资源
        最近更新 更多