硒测试不能与任何元素相互作用 [英] Selenium tests are not able to interact with any of the elements
问题描述
所以我知道,当我在Django设置中将DEBUG设置为False时,Selenium无法访问静态文件,结果如下所示:
So I know that when I have DEBUG in my Django settings set to False, Selenium fails to have access to the static files, resulting in something looking like:
但是,当我运行Selenium测试时,无论它们是否能够与DOM交互并从下拉列表中选择项目!我目前拥有的测试代码
However, when I run my Selenium tests regardless they are able to interact with the DOM and select items from the dropdown! The test code I have is currently
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时,页面将呈现我想要的样子,因为它可以访问静态文件.但是,每当我尝试运行测试时,我总会得到错误
When I set DEBUG to True, the page renders how I want it to because it has access to the static files. But whenever I try to run the tests I always get the error
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
我所指的任何元素似乎都发生了这种情况.由于某些原因,当我尝试直接单击下拉菜单时,它总是突出显示此输入,并且select标记似乎总是被隐藏",因此我想知道这是否可能导致错误
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直接访问select标签进行下拉.
Thank you all for your help, I don't know why Selenium is not being allowed to directly access the select tag for drop down.
推荐答案
您的页面使用的自定义选择组件不是默认的 MDBootstrap Select组件,它不能与硒Select
进行交互课
Your page uses custom Select Component which is not the default html select box. In your case, they have used MDBootstrap Select Component which cannot be interacted using selenium Select
class
你是对的.您正在尝试与不可见的<select>
dom交互,并且它抛出元素不可见的异常.
我们有两种自动执行此案例的方法,与手动操作完全一样,
You are right. You are trying to interact with <select>
dom which is not visible and it is throwing element not visible exception.
We have two automate this case exactly similar like the manual steps,
即
- 单击难以处理的元素.
- 等待下拉菜单.
- 单击下拉菜单中的值.
- Click the element which is intractable.
- Wait for the dropdown appears.
- Click the value from the dropdown.
在舞蹈选择框中选择第一个值的情况下,代码可以重写如下.
In your case for selecting first value in dance select box, the code can rewritten as below.
# 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()
这篇关于硒测试不能与任何元素相互作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!