空手道 - 从 <a> 获取 URL使用 Javascript 的元素 [英] Karate - get URL from <a> element using Javascript
问题描述
使用空手道,我需要从标签 (href) 中提取 url.我已经在浏览器中直接使用 Javascript 进行了尝试:
Using Karate, I need to extract the url from tag (href). I have tried this using Javascript directly in browser:
document.querySelector('a[rel="noopener"]').href
它在浏览器中运行良好,我收到完整的 url 地址(https://XXXXXXXX").然后我想把它放到空手道上:
It works in browser fine, I receive whole url address ("https://XXXXXXXX"). Then I wanted to place it to Karate using:
* def emailLink =
"""
function(){
var link = document.querySelector('a[rel="noopener"]').href;
return link;
}
"""
* print emailLink()
但它不起作用.变量=".我需要接收 href 值.
But it does not work. The variable = "". I need to receive the href value.
你知道如何解决这个问题吗?
Have you any Idea how to solve this?
谢谢!
推荐答案
你写的函数不工作,请阅读这里了解原因:https://github.com/intuit/karate/tree/master/karate-core#karate-vs-the-浏览器
The function you are writing will not work, and please read this to understand why: https://github.com/intuit/karate/tree/master/karate-core#karate-vs-the-browser
因此进行此更改(并注意空手道如何为您执行 document.querySelector()
):
So make this change (and also note how Karate does document.querySelector()
for you):
* def linkElement = locate('a[rel="noopener"]')
* def linkValue = linkElement.attribute('href')
请注意,对于第二行,这也应该有效:
Note that for the second line this should also work:
* def linkValue = linkElement.script('_.href')
甚至这个:
* def linkValue = linkElement.property('href')
开始习惯空手道中的 Element
抽象.它可能非常有用:https://github.com/intuit/空手道/树/大师/空手道核心#chaining
Start getting used to the Element
abstraction in Karate. It can be very useful: https://github.com/intuit/karate/tree/master/karate-core#chaining
这篇关于空手道 - 从 <a> 获取 URL使用 Javascript 的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!