使用python-selenium可以在Shadow DOM中查找元素吗?
示例用例:
我有一个type="date"
的input
:
<input type="date" name="bday">
我想要点击右侧的日期选择器按钮,并从日历中选择一个日期。
如果您在Chrome开发者工具中检查该元素并展开日期输入的shadow-root节点,您会看到该按钮显示为:
<div pseudo="-webkit-calendar-picker-indicator" id="picker"></div>
在 Chrome 中的外观截图:
NoSuchElementException
:>>> date_input = driver.find_element_by_name('bday')
>>> date_input.find_element_by_id('picker')
...
selenium.common.exceptions.NoSuchElementException: Message: no such element
我也尝试过使用建议这里的::shadow
和/deep/
定位器:
>>> driver.find_element_by_css_selector('input[name=bday]::shadow #picker')
...
selenium.common.exceptions.NoSuchElementException: Message: no such element
>>>
>>> driver.find_element_by_css_selector('input[name=bday] /deep/ #picker')
...
selenium.common.exceptions.NoSuchElementException: Message: no such element
请注意,我可以通过向输入框发送按键来更改日期:
driver.find_element_by_name('bday').send_keys('01/11/2014')
但是,我想通过从日历中选择日期来具体设置日期。