HTML 字符串未得到验证

发布于 2024-10-08 19:57:59 字数 933 浏览 4 评论 0原文

这些空格不是我在 HTML SIDE 上添加的,我无法编辑 HTML 我想知道我的比较字符串应该是什么?

我正在使用 watin 来自动化网站测试过程,但我无法只遇到一个按钮。所有其他工作

watin 按名称/值/id 等搜索内容并工作很好,但是当我看到我需要单击的提交按钮的值时,它有一些中断 &nsbp 所以我认为它们正在发挥一些作用

这是 html:

<span class='button'><input type="submit" value="&nbsp;&nbsp;&nbsp;Login&nbsp;&nbsp;&nbsp;" /></span>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span class='button'><input type="button" value="&nbsp;&nbsp;&nbsp;Back&nbsp;&nbsp;&nbsp;" onclick="history.back(-1)" /></span> 

代码?

  browser.Button(WatiN.Core.Find.ByValue("&nbsp;&nbsp;&nbsp;Login&nbsp;&nbsp;&nbsp;")).Click();   

这是搜索可以做什么的

These spaces are not added by me on HTML SIDE and i cannot edit HTML
I want to know what should my comparison string?

I am using watin to automate website testing process but I am unable to encounter only one button.Every other works

watin searches content by name /values /id and many more and works fine but when i see the value of the submit button that i need to be clicked it has some breaks &nsbp so i think they are playing some role

Here is the html:

<span class='button'><input type="submit" value="   Login   " /></span>   
         
      
<span class='button'><input type="button" value="   Back   " onclick="history.back(-1)" /></span> 

and here is the code to search

  browser.Button(WatiN.Core.Find.ByValue("   Login   ")).Click();   

what can be done??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

污味仙女 2024-10-15 19:57:59

-- 建议 -- (即太大而无法发表评论)

您不应使用   向提交按钮添加空格。相反,您应该使用 CSS 根据您的喜好设置按钮的样式。因此,您会得到类似的结果:

input[type=button] {
    padding:10px;
    min-width: 150px;
}

出于同样的原因,这可以消除您在选择按钮时遇到的任何问题。这可能是编码破坏 watin 的问题,因此,使用 CSS 执行此操作将使调试问题变得更加清晰和容易。

编辑:
您是否尝试过按 ID 而不是按值进行搜索? ID 在页面上应该是唯一的,因此如果通过这些方式找不到它,那么这是一个可以排除的问题。也可能是您正在寻找一个按钮。

编辑 2: 即使问题是由于编码中断造成的,我仍然建议您重置该按钮以重置文本(删除所有非中断空格)并为其附加 id/名称。原因是出于国际化目的 - 如果由于某种原因您修改了设计器中按钮的大小,或者国际化了应用程序并且文本不同,您的测试将会中断。

-- Suggestion -- (i.e. too big for a comment)

You shouldn't use   to add spaces to the submit button. Rather, you should use CSS to style the button to your liking. So you would have something like:

input[type=button] {
    padding:10px;
    min-width: 150px;
}

By the same token, this could eliminate any of the issues you're having with selecting the button. It could be an issue of encodings breaking with watin and as a result, doing this with CSS will make debugging the issue much cleaner and much easier.

Edit:
Have you tried searching by ID as opposed to by value? ID's are supposed to be unique on a page, so if it doesn't find it by those means, then that's one issue that can be rules out. It could also be the fact that you're searching for a button. A <button> is not the same as a <input type="button">.

Edit 2: Even though the issue was due to encodings breaking, I still recommend you reset that button to reset the text (removing all the non breaking spaces) and attach an id/name to it. The reason being for internationalization purposes - and if for some reason you modify the size of the button in the designer, or i18n the app and the text is different, your test will break.

月光色 2024-10-15 19:57:59

您不应该将实体与 WatiN 一起使用。

此代码可以工作,但您必须使用真正的不间断空格字符

browser.Button(
    WatiN.Core.Find.ByValue(
        "   Login   ")).Click();

这可能不方便,但您可以使用(在添加对 System.Web 的引用后)< code>HttpUtility 类:

browser.Button(
    WatiN.Core.Find.ByValue(
        System.Web.HttpUtility.HtmlDecode(
            "   Login   "))).Click();

但是,如果我是你,我会选择 Regex:

browser.Button(
    WatiN.Core.Find.ByValue(
        new Regex(@"^\s*Login\s*$"))).Click();

甚至 new Regex("Login")

有趣的是:如果您必须Find.ByText,您不必费那么多功夫,您可以使用常规空格(即不完全是不间断空格)。这是因为本机 IE IHTMLElement::getAttribute (http://msdn.microsoft.com/en-us/library/aa752280(VS.85).aspx) 转换  < /code> 从 innertext 属性到常规空格,但从 valueid 等,它不是 (  转换为真正的不间断空格 - 0xA0

You shouldn't use entities with WatiN.

This code will work, but you have to use real non-breaking space character:

browser.Button(
    WatiN.Core.Find.ByValue(
        "   Login   ")).Click();

This is probably inconvenient, but you could use (after adding reference to System.Web) HttpUtility class:

browser.Button(
    WatiN.Core.Find.ByValue(
        System.Web.HttpUtility.HtmlDecode(
            "   Login   "))).Click();

But, if I were you, I would just go with Regex:

browser.Button(
    WatiN.Core.Find.ByValue(
        new Regex(@"^\s*Login\s*$"))).Click();

or even new Regex("Login").

Interesting thing: If you ever will have to Find.ByText you don't have to bother so much, and you can use regular space (ie. not exactly non-breaking space). That's because native IE IHTMLElement::getAttribute (http://msdn.microsoft.com/en-us/library/aa752280(VS.85).aspx) converts   from innertext attribute to regular spaces, but from value, id etc. it doesn't (  are converted to real non-breaking spaces - 0xA0)

等待我真够勒 2024-10-15 19:57:59

哇,你真的很喜欢空间!我会删除它们并使用填充/边距,就像 html 的设计目的一样。然后你就不需要所有这些空格,你可以为你的按钮分配一个正确的值,watiN 会识别它。

Wow, you really like spaces! I would remove those and use padding/margins like html was designed to be used. Then you wont need all those spaces and you can assign a proper value to your button which watiN will recognize.

木格 2024-10-15 19:57:59

我认为这是因为 HTML 源代码中的   实际上是表示不间断空格的特殊字符的转义版本。因此,在 C# 源代码中,您可能需要该字符而不是 html 实体代码。我认为您可以通过使用此按钮提交 GET 表单来找到该字符的代码。它将在 url 中显示转义字符代码。

当然,最好不要在其中放置空格。您应该使用 CSS 为按钮提供填充。

I think it is because the   in the HTML source is actually an escaped version of the special character that represents a none breaking space. So in you C# source, you'll probably need that character instead of the html entity code. I think you can find the code of that character by using this button to submit a GET form. It will show the escaped character code in the url.

Of course it is better not to put the spaces in there at all. You should give the button a padding using CSS instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文