Cypress:如何从包含HTML的变量中获取元素?

huangapple go评论64阅读模式
英文:

Cypress: How to get elements from a variable with HTML?

问题

我正在进行API测试。我正在测试的API返回一个HTML主体。如何从响应主体中获取元素?如果我访问链接,它会返回一个iframe,而我无法在cy.origin内正确访问它。

如何获取https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193dXudSov7Y6JDm?我已经花了几个小时查找解决方案,但似乎找不到解决方案 :/

var message = `<html>
    
<body style="background-color:#f5f5f5;font-family:'Open Sans',sans-serif;padding:24px 0;">
    <div style="margin:0 auto 24px auto;text-align:center">

        <img width="130" src="cid:quipper_logo" alt="School LINK" />

  </div>
        <hr style="border:none;" />
        <div style="margin:12px auto;max-width:540px;background-color:white;border:1px #dcdcdc solid;padding:20px;">
            <p><strong>** This email is automatically generated. Please do not reply. **</strong></p>

            <p>Your Excel spreadsheet for is ready to download.</p>

            <p>Please note: you will need to enter the supplied username and password to download the file.</p>

            <p><strong>Download:</strong> <a
                    href="https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193d">https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193d</a>
            </p>

            <p>Username: test@test.com</p>
            <p>Password: XudSov7Y6JDm</p>

        </div>
</body>

</html>`

希望这可以帮助你。

英文:

I'm currently working on API testing. The API that I'm testing is returning an HTML body. How do I get an element from the response body? If I visit the link, it returns an iframe and I cannot access it properly inside cy.origin.

How to get the https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193d or the XudSov7Y6JDm ? I've been going through this for hours but can't seem to find a solution :/

var message = `<html>

<body style="background-color:#f5f5f5;font-family:'Open Sans',sans-serif;padding:24px 0;">
	<div style="margin:0 auto 24px auto;text-align:center">

		<img width="130" src="cid:quipper_logo" alt="School LINK" />

  </div>
		<hr style="border:none;" />
		<div style="margin:12px auto;max-width:540px;background-color:white;border:1px #dcdcdc solid;padding:20px;">
			<p><strong>** This email is automatically generated. Please do not reply. **</strong></p>


			<p>Your Excel spreadsheet for is ready to download.</p>



			<p>Please note: you will need to enter the supplied username and password to download the file.</p>




			<p><strong>Download:</strong> <a
					href="https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193d">https://test-develop.test.net/download/cd668080-be63-11ed-8328-41d4c5da193d</a>
			</p>


			<p>Username: test@test.com</p>
			<p>Password: XudSov7Y6JDm</p>



		</div>
</body>

</html>`

答案1

得分: 3

请查看此答案 如何检查HTML文档中每个脚本标记的src属性,其中使用 DOMParser 从HTML字符串中提取元素。

在您的情况下有一个 <a>,所以略有不同

const parser = new DOMParser();
const doc = parser.parseFromString(message, 'text/html')
const href = doc.querySelector('a').href

这基本上是实现的方法,但我有点担心对 cy.origin() 的引用,因为在该命令内有一些不起作用的情况。

无论如何,试一下,如果不起作用,请更新该原点调用的详细信息。

英文:

Please see this answer How can I check the src attribute of every script tag in an html document that uses DOMParser to extract elements from the HTML string.

In your case there's one &lt;a&gt;, so slightly different

const parser = new DOMParser();
const doc = parser.parseFromString(message, &#39;text/html&#39;)
const href = doc.querySelector(&#39;a&#39;).href

This is broadly the way to do it, but I'm a bit worried about the reference to cy.origin() as there are some things that don't work inside that command.

In any case give it a spin, if not working update details of that origin call.

答案2

得分: 1

你可以在Python中使用regex

import re

message = # 插入你的HTML正文
link = re.search(r'https://\S+', message).group(0)
password = re.search(r'Password: (\S+)', message).group(1)
print(download_link)
print(password)
英文:

You can use the regex in python:

import re

message = # insert you HTML Body 
link = re.search(r&#39;https://\S+&#39;, message).group(0)
password = re.search(r&#39;Password: (\S+)&#39;, message).group(1)
print(download_link)
print(password)

huangapple
  • 本文由 发表于 2023年3月9日 19:53:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定