在使用Selenium构建python脚本时,我遇到了与网站的特定部分进行交互的问题。通过从Selenium对象(chrome驱动程序)中转储element:attribute列表,我可以看到“ ko foreach”部分中的所有内容都不存在。
<oc-drawer-panel id="rightDrawerPanel" right-drawer="" force-narrow="" disable-edge-swipe="" disable-swipe="" drawer-toggle-attribute="oc-drawer-toggle-right" drawer-width="90%" narrow="" selected="main" style="touch-action: auto;">
<div id="mainContent">
<iron-pages id="workspacePages" data-bind="attr: {selected: selectedWorkspaceIndex}" class="oc-pages-content" attr-for-selected="index" selected="1">
<!-- ko ocForEach: workspaces -->
<div data-oc-foreach-node="" data-bind="attr: {index: $index()}" index="0" class="">
<oc-workspace-tab-control data-oc-foreach-ignore-children="" data-oc-foreach-node="" selected-tab="0" id="workspaceTabControl" data-bind="ocPropertyChanged:{viewModel: $data}"></oc-workspace-tab-control>
</div>
<div data-oc-foreach-node="" data-bind="attr: {index: $index()}" index="1" class="iron-selected">
<oc-workspace-tab-control data-oc-foreach-ignore-children="" data-oc-foreach-node="" selected-tab="0" id="workspaceTabControl" data-bind="ocPropertyChanged:{viewModel: $data}"></oc-workspace-tab-control>
</div>
<div data-oc-foreach-node="" data-bind="attr: {index: $index()}" index="2">
<oc-workspace-tab-control data-oc-foreach-ignore-children="" data-oc-foreach-node="" selected-tab="0" id="workspaceTabControl" data-bind="ocPropertyChanged:{viewModel: $data}"></oc-workspace-tab-control>
</div>
<div data-oc-foreach-node="" data-bind="attr: {index: $index()}" index="3">
<oc-workspace-tab-control data-oc-foreach-ignore-children="" data-oc-foreach-node="" selected-tab="0" id="workspaceTabControl" data-bind="ocPropertyChanged:{viewModel: $data}"></oc-workspace-tab-control>
</div>
<!-- /ko -->
</iron-pages>
</div>
如果我在Chrome Inspector中右键单击目标,复制JS Path,然后粘贴到控制台中,则会出现以下错误:
Uncaught TypeError: Cannot read property 'shadowRoot' of null
at <anonymous>:1:284 (anonymous) @ VM11437:1
我也在Firefox中尝试过。如果单击“在控制台中使用”,则可以使用js方法与var元素成功交互,但是当我尝试解析DOM路径时,我将无法定义:
function getDomPath(el) {
var stack = [];
while ( el.parentNode != null ) {
console.log(el.nodeName);
var sibCount = 0;
var sibIndex = 0;
for ( var i = 0; i < el.parentNode.childNodes.length; i++ ) {
var sib = el.parentNode.childNodes[i];
if ( sib.nodeName == el.nodeName ) {
if ( sib === el ) {
sibIndex = sibCount;
}
sibCount++;
}
}
if ( el.hasAttribute('id') && el.id != '' ) {
stack.unshift(el.nodeName.toLowerCase() + '#' + el.id);
} else if ( sibCount > 1 ) {
stack.unshift(el.nodeName.toLowerCase() + ':eq(' + sibIndex + ')');
} else {
stack.unshift(el.nodeName.toLowerCase());
}
el = el.parentNode;
}
return stack.slice(1); // removes the html element
}
var path = getDomPath(temp1);
console.log(path.join(' > '));
PAPER-BUTTON debugger eval code:4:13
DIV debugger eval code:4:13
paper-button#btn debugger eval code:30:9
undefined
如果我在firefox控制台中的js路径出现此错误:
document.querySelector("#frameParent > oc-frame > div > oc-dynamic-control-container > oc-workspaces-tabs").shadowRoot.querySelector("#workspaceTabControl").shadowRoot.querySelector("#tabPageContent > div > div:nth-child(3) > div > oc-dynamic-control-container > oc-search-criteria").shadowRoot.querySelector("#commandBarContainer > oc-command-bar > oc-command-bar-item:nth-child(3)").shadowRoot.querySelector("#btn")
TypeError: document.querySelector(...).shadowRoot.querySelector(...).shadowRoot.querySelector(...) is null
关于如何访问这些元素的任何想法?