Iframe 没有这样的元素错误:使用locator:By(css选择器,button.src-component-launcher-widgetluncher-wrapper.u-is)找不到元素

Iframe 没有这样的元素错误:使用locator:By(css选择器,button.src-component-launcher-widgetluncher-wrapper.u-is)找不到元素,iframe,protractor,cucumber,Iframe,Protractor,Cucumber,我已经进入了框架,但当我试图找到一个元素时,我无法找到元素 iframe标记的代码如下所示: <iframe title="Opens a widget where you can find more information" id="launcher" tabindex="0" class="zEWidget-launcher zEWidget-launcher--active" style="border: none; background: transparent; z-index:

我已经进入了框架,但当我试图找到一个元素时,我无法找到元素

iframe标记的代码如下所示:

<iframe title="Opens a widget where you can find more information" id="launcher" tabindex="0" class="zEWidget-launcher zEWidget-launcher--active" style="border: none; background: transparent; z-index: 999998; transform: translateZ(0px); position: fixed; transition: opacity 250ms cubic-bezier(0.645, 0.045, 0.355, 1) 0s, top, bottom; opacity: 1; width: 113px; height: 50px; max-height: 551px; min-height: 50px; margin: 10px 20px; right: 0px; bottom: 0px;"></iframe>
<button class="src-component-launcher-WidgetLauncher-wrapper u-isActionable u-textLeft u-inlineBlock u-borderNone u-textBold u-textNoWrap Arrange Arrange--middle u-userLauncherColor "><span data-testid="Icon" class="src-component-Icon-container u-userColor src-component-launcher-WidgetLauncher-icon src-styles-components-Icon-Icon Arrange-sizeFit u-textInheritColor u-inlineBlock  Icon" type="Icon"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" style="enable-background:new 0 0 20 20"><path d="M11,12.3V13c0,0-1.8,0-2,0v-0.6c0-0.6,0.1-1.4,0.8-2.1c0.7-0.7,1.6-1.2,1.6-2.1c0-0.9-0.7-1.4-1.4-1.4 c-1.3,0-1.4,1.4-1.5,1.7H6.6C6.6,7.1,7.2,5,10,5c2.4,0,3.4,1.6,3.4,3C13.4,10.4,11,10.8,11,12.3z"></path><circle cx="10" cy="15" r="1"></circle><path d="M10,2c4.4,0,8,3.6,8,8s-3.6,8-8,8s-8-3.6-8-8S5.6,2,10,2 M10,0C4.5,0,0,4.5,0,10s4.5,10,10,10s10-4.5,10-10S15.5,0,10,0 L10,0z"></path></svg></span><span class="src-component-launcher-WidgetLauncher-label Arrange-sizeFit u-textInheritColor u-inlineBlock " data-testid="launcherLabel">Help</span></button>

但它不起作用。

可能是您缺少先切换到iframe的代码,或者选择器不是唯一的,并且它进入了错误的iframe。建议您获取文本并
console.log
查看它是否确实是正确的iframe

也就是说,下面的代码工作得很好。它基本上使用了与您相同的定位器,但是在转移焦点之后

browser.waitForAngularEnabled(false);
browser.sleep(5000);

browser.switchTo().frame(element(by.tagName('iframe#launcher')).getWebElement());
element(by.css('button.src-component-launcher-WidgetLauncher-wrapper.u-isActionable.u-textLeft.u-inlineBlock.u-borderNone.u-textBold.u-textNoWrap.Arrange.Arrange--middle.u-userLauncherColor'))
                    .getText().then((text) => {
                        console.log(text);
});
参考:

更新: 通常情况下,iframe很难使用。即使它们在角度页面中使用,父页面角度页面也无法控制iframe中的内容。此外,父页面加载完成并不意味着加载了iframe。更新了代码以将其包括在内。应该可以正常工作


希望对您有所帮助。

在访问内部的任何元素之前,您需要首先切换到iframe

browser.switchTo().frame($("#launcher").getWebElement())
上面的代码将焦点切换到iframe窗口。现在您可以使用
by.buttonText()
by.partialButtonText()
定位器找到该按钮

element(by.partialButtonText("Help")).click()
现在,您可以使用将焦点更改为原始页面

browser.switchTo().defaultContent()

实际上,我想点击“帮助”按钮。这些全部信息都是关于网站的:。请查看网站,按钮在右下角。@RAJKUMAR更新了答案。一般来说,最好看看发生错误时量角器到底在处理什么。在这种情况下,您可以检查iframe是否已加载。这会提示您尚未加载iframe。
元素(by.css('body')).all(by.css('iframe')).count()。然后((val)=>{console.log(“iframes已加载:+val)})
。我们越是远离调试/探测页面内容,问题就越有可能变得令人费解。谢谢,这一切都很好,我知道了如何切换到我想要的确切框架。
browser.switchTo().defaultContent()