本文介绍了空手道-从< a>获取URL.使用Javascript的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用空手道,我需要从标记(href)中提取网址.我已经在浏览器中直接使用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的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 17:18