getAttribute isPermaLink 正在使用 Protractor 为 rss 提要中的元素返回 null
getAttribute isPermaLink is returning null for element in rss feed with Protractor
我尝试了很多方法来使用量角器获取属性 isPermaLink 的值。
我可以很好地获取任何其他元素的值,但是 isPermaLink 总是 returns null..
HTML
<guid isPermaLink="false">public-anger-grows-over-coronavirus-123829864.html</guid>
代码
const isPerma = element(by.xpath('//guid[@isPermaLink]')).
console.log('isPermaLink value ', await isPerma.getAttribute('isPermaLink'));
如果我尝试其他元素,比如 source 标签,我可以获得值
<source url="http://www.ap.org/">Associated Press</source>
位于开发工具中的元素
Link 到正在使用的 Yahoo Rss 提要: https://news.yahoo.com/rss/
答案(或者说我对问题的理解):
- 如何获取属性?
let $permaLink = $$('guid[isPermaLink]').get(0);
let attr = await browser.executeScript('return arguments[0].getAttribute("isPermaLink")', $permaLink.getWebElement());
console.log(attr) // false
- 为什么
await element.getAttribute('isPermaLink')
不起作用?
假设您在 html 页面中有一个 iframe
,并且您正在寻找此框架内的元素。在这种情况下,您可以在浏览器的控制台中与该元素交互(找到它等),但是从量角器端您只能在 browser.switchTo().frame(element(by.tagName('iframe')).getWebElement());
之后与它交互
在您的情况下,您在 html 页面内有一个 xml,其行为类似。但是,量角器仅适用于文档本身(不适用于文档内部的文档)。问题是如果您尝试 switchTo
,您将得到一个错误 no such frame: element is not a frame
。因为该方法被设计为仅适用于 iframe,但概念是相同的
我尝试了很多方法来使用量角器获取属性 isPermaLink 的值。
我可以很好地获取任何其他元素的值,但是 isPermaLink 总是 returns null..
HTML
<guid isPermaLink="false">public-anger-grows-over-coronavirus-123829864.html</guid>
代码
const isPerma = element(by.xpath('//guid[@isPermaLink]')).
console.log('isPermaLink value ', await isPerma.getAttribute('isPermaLink'));
如果我尝试其他元素,比如 source 标签,我可以获得值
<source url="http://www.ap.org/">Associated Press</source>
位于开发工具中的元素
Link 到正在使用的 Yahoo Rss 提要: https://news.yahoo.com/rss/
答案(或者说我对问题的理解):
- 如何获取属性?
let $permaLink = $$('guid[isPermaLink]').get(0);
let attr = await browser.executeScript('return arguments[0].getAttribute("isPermaLink")', $permaLink.getWebElement());
console.log(attr) // false
- 为什么
await element.getAttribute('isPermaLink')
不起作用?
假设您在 html 页面中有一个 iframe
,并且您正在寻找此框架内的元素。在这种情况下,您可以在浏览器的控制台中与该元素交互(找到它等),但是从量角器端您只能在 browser.switchTo().frame(element(by.tagName('iframe')).getWebElement());
在您的情况下,您在 html 页面内有一个 xml,其行为类似。但是,量角器仅适用于文档本身(不适用于文档内部的文档)。问题是如果您尝试 switchTo
,您将得到一个错误 no such frame: element is not a frame
。因为该方法被设计为仅适用于 iframe,但概念是相同的