所以我知道当我将 Django 设置中的 DEBUG 设置为 False 时,Selenium 无法访问静态文件,导致如下所示:
但是,当我运行 Selenium 测试时,无论它们都能够与 DOM 交互并从下拉列表中选择项目!我目前拥有的测试代码是
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from urllib.parse import urljoin
import time
driver = webdriver.Chrome()
driver.get("localhost:8000/")
time.sleep(3)
driver.find_element_by_id('select-dance').click()
select = Select(driver.find_element_by_xpath('//*[@id="select-dance"]'))
select.select_by_value('1')
driver.find_element_by_id('select-date-range').click()
select = Select(driver.find_element_by_xpath('//*[@id="select-date-range"]'))
select.select_by_value('1')
driver.find_element_by_id('location').click()
当我将 DEBUG 设置为 True 时,页面会按照我想要的方式呈现,因为它可以访问静态文件。但每当我尝试运行测试时,我总是会收到错误
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
This seems to happen for any element I refer to. For some reason, when I try to click on the dropdown directly, it always highlights this input and the select tag always seems to be 'hidden', so I wonder if that may be causing the error
谢谢大家的帮助,我不知道为什么不允许 Selenium 直接访问下拉菜单的选择标签。
您的页面使用自定义选择组件,该组件不是默认组件html选择框 https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select。就你而言,他们已经使用了MDBootstrap 选择组件 https://mdbootstrap.com/javascript/material-select/不能使用 selenium 进行交互Select
class
你是对的。您正在尝试与<select>
dom 不可见,并且抛出元素不可见异常。
我们有两个自动化的案例,与手动步骤完全相似,
i.e.,
- 单击难以处理的元素。
- 等待下拉菜单出现。
- 单击下拉列表中的值。
在您选择舞蹈选择框中的第一个值的情况下,代码可以重写如下。
# this is click the input element which is intractable
# Here the input box which contains value 'dance event' is clicked
driver.find_element_by_css_selector('input.select-dropdown[value*="dance event"]').click
# Then we are waiting for the first value of the dropdown which is not disabled
wait = WebDriverWait(driver, 60)
element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input.select-dropdown[value*="dance event"]+ul>li:not(.disabled)')))
# Click the element first value of the dropdown
element.click()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)