简体   繁体   English

Firefox C# Selenium 无法使用 sendKeys 设置值,JavaExecutor 似乎没有影响它

[英]Firefox C# Selenium Can't set a value using sendKeys and JavaExecutor doesn't seem to be affecting it

Hi all,大家好,

Bit of a conundrum here for me, been at it a while to get past this issue but am just seemingly stuck.对我来说这里有点难题,为了解决这个问题已经有一段时间了,但似乎被困住了。 I am trying to write an automation strategy for a Trading Platform (In this case Trading212) and attempting to use C# with Selenium in order to do so.我正在尝试为交易平台(在本例中为 Trading212)编写自动化策略,并尝试将 C# 与 Selenium 一起使用。

All was going fine until I got to the overlay that deals with Placing an order.一切都很好,直到我到达处理下订单的覆盖。 Much like normal I tried sendKeys but that responded badly with all attempts:就像平常一样,我尝试了 sendKeys 但所有尝试都反应不佳:

Errors错误

-Error ElementNotInteractableException (Can't be scrolled into view) -Error ElementNotInteractableException(无法滚动到视图中)
-Error ElementNotInteractableException (Element is not reachable by keyboard) -Error ElementNotInteractableException(键盘无法访问元素)
-js.ExecuteScript("arguments[0].value='12345';", TargetPriceInput); -js.ExecuteScript("arguments[0].value='12345';", TargetPriceInput); (Changes a value just not the value) (改变一个值而不是值)

The bit I'm trying to adjust is the Price which apparantly can not be modified from any way I can find and it was hard enough to select as it has a dynamically changing ID, no class to hang on to was only able to get it by finding an input with "Text".我要调整的一点是价格,它显然无法以我能找到的任何方式进行修改,而且对于 select 来说已经够难了,因为它有一个动态变化的 ID,没有 class 只能坚持下去通过查找带有“文本”的输入。

Element I am trying to update:我正在尝试更新的元素:

    <div class="spinner _focusable" tabindex="-1" data-dojo-attach-event="onkeydown: onKeyDown" id="uniqName_0_131" widgetid="uniqName_0_131">
        <span class="spinner-placeholder" data-dojo-attach-point="placeholderNode">Price</span>
        <input type="text" autocomplete="off" id="uniqName_0_132" widgetid="uniqName_0_132">
        <div class="spinner-arrow-container">
            <div class="spinner-arrow spinner-down svg-icon-holder" data-dojo-attach-point="spnLeftNode"><svg class="svg-icon dropdown-arrow-down-icon" width="32" height="32" viewBox="0 0 32 32">
        <g fill="none" fill-rule="evenodd">
            <polyline stroke="#6F6F7F" points="20.5 14.5 16 19 11.5 14.5" stroke-linecap="round"></polyline>
        </g>
    </svg>
    </div>
            <div class="spinner-arrow spinner-up svg-icon-holder" data-dojo-attach-point="spnRightNode"><svg class="svg-icon dropdown-arrow-up-icon" width="32" height="32" viewBox="0 0 32 32">
      <g fill="none" fill-rule="evenodd">
        <polyline stroke="#6F6F7F" points="20.5 18.5 16 14 11.5 18.5" stroke-linecap="round"></polyline>
      </g>
    </svg></div>
        </div>
        <div class="spinner-disabled-click-catcher" data-dojo-attach-point="clickCatcherNode"></div>
    </div>

It seems like it is tied with Javascript to update when a key is pressed and the box is selected but I can't seem to get selenium to select the box nor push data into it.似乎它与 Javascript 捆绑在一起,以便在按下一个键并选择该框时进行更新,但我似乎无法将 selenium 转换为 select 框,也无法将数据推送到该框。 There are events tied to the input box but can't seem to find a way to make it go.有些事件与输入框相关,但似乎无法找到使其成为 go 的方法。

                    TargetPrice.Click();
                    TargetPrice.SendKeys("05485.00000");

Both just give errors and refuse to do the job.两者都只是给出错误并拒绝完成工作。 original look of the panel面板的原始外观

Firefox Inspector also shows me the value of the Element is indeed a string like value: "1.31407" which is hidden but I definitely was able to update with ExecuteScript but it is not Validating it. Firefox Inspector 还向我展示了 Element 的值确实是一个类似于值的字符串:“1.31407”,它是隐藏的,但我确实能够使用 ExecuteScript 进行更新,但它没有验证它。 I can see the value is updated but when I click anywhere else it just gets dumped.我可以看到该值已更新,但是当我单击其他任何地方时,它就会被丢弃。

Looking into it further it seems there are some scripts probably JavaScript that manually evaluate what buttons I am pressing and resolve it.进一步研究它似乎有一些脚本可能是 JavaScript 手动评估我按下的按钮并解决它。 Not sure how to get around that.不知道如何解决这个问题。

Event OnKeyDown事件 OnKeyDown

function, o = i.checkIntegerPartLength, s = i.onKeyPressed, l = this.state, d = l.currentValue, u = l.maxInputLength, h = this.getPrecision(), f = !1;
for (var p in r.default)
  if (void 0 === v[p] && r.default[p] === t.keyCode) {
    f = !0;
    break
  }(t.altKey || 18 === t.keyCode) && (t.stopPropagation(), t.preventDefault());
var g = String.fromCharCode(t.keyCode);
!f && isNaN(parseInt(g, 10)) && 190 != t.keyCode && 110 != t.keyCode && 109 != t.keyCode && 189 != t.keyCode && (t.stopPropagation(), t.preventDefault()), 190 != t.keyCode && 110 != t.keyCode || 0 != h && -1 === this.domNode.value.indexOf(".") || (t.stopPropagation(), t.preventDefault()), 189 != t.keyCode && 109 != t.keyCode || (this.onMinusPress(), t.stopPropagation(), t.preventDefault()), t.shiftKey && !isNaN(parseInt(g, 10)) && (t.stopPropagation(), t.preventDefault());
var m = (g = t.keyCode || t.charCode) == r.default.HOME || g == r.default.END || g == r.default.DELETE || g == r.default.BACKSPACE || g >= 37 && g <= 40;
if (!m && !o && c.default.getLength(this.domNode.value.replace(/\s/g, "")) >= u + h && this.domNode.selectionStart == this.domNode.selectionEnd && t.preventDefault(), !(a || t.keyCode !== r.default.NUMPAD_0 && f)) {
  var C = this.domNode.value.split("."),
    y = n(C, 1)[0],
    b = 48 == t.keyCode || 96 == t.keyCode,
    w = this._getTextSelection(this.domNode),
    T = w.start,
    L = w.length,
    _ = this.domNode.value.length === L;
  0 === T && "." !== this.domNode.value.charAt(L) && !_ && y.length && b && t.preventDefault()
}
if (o && !m && 190 !== t.keyCode && 110 !== t.keyCode) {
  var N = this.domNode.value.split("."),
    S = n(N, 2),
    E = S[0],
    I = S[1],
    x = this.domNode.value.indexOf("."),
    M = this._getTextSelection(this.domNode),
    O = M.start,
    P = M.length,
    A = E.substr(O, P),
    D = A && (A.length > 1 || 160 !== A.charCodeAt(0));
  E.replace(/\s/g, "").length >= u && !D && (!I && -1 === x || O <= x) && t.preventDefault()
}
"0" != d || h || m || 48 != t.keyCode && 96 != t.keyCode || t.preventDefault(), 65 == g && t.ctrlKey && this.domNode.select(), setTimeout((function() {
  e._destroyed || (s(), e.updateState(e.domNode.value, {
    isUserTyping: !0,
    keepCursorPosition: !0
  }))
}), 5)
}, onFocusEvent: function() {
    var t = this.props,
      e = t.o

Happy to provide as much details as needed.很高兴根据需要提供尽可能多的详细信息。 The page I'm dealing with is the demo webpage for trading212 so freely publically accessible if you happen to know how to make it go.我正在处理的页面是 trading212 的演示网页,如果您碰巧知道如何使其成为 go,则可以免费公开访问。

Tried already to do virtual keystrokes using a separate library but even windows based seem to not be getting picked up weirdly.已经尝试使用单独的库进行虚拟击键,但即使基于 windows 似乎也没有被奇怪地接受。

EDIT: After a long hard look and some decent retesting using the XPath system advised in the answer below I managed to find out that the reason none of it made sense if because despite my best efforts the Element being attempted to be modified was NOT the correct one but a similar one.编辑:经过长时间的仔细观察和使用 XPath 系统在下面的答案中建议的一些体面的重新测试后,我设法找出原因没有任何意义,因为尽管我尽了最大努力,但尝试修改的元素不是正确的一个但类似的。

To resolve even if in a temporary way I went into firefox inspected, selected the target input field and right clicked, then I clicked to copy -> XPath.为了解决,即使我以临时方式进入 firefox 检查,选择目标输入字段并右键单击,然后单击复制-> XPath。

This gave me a direct route to the correct XPath.这给了我一条通往正确 XPath 的直接路线。
IWebElement XPathInput = driver.FindElement(By.XPath("/html/body/div[7]/div[2]/div[3]/div[2]/div[1]/div[3]/div/div[2]/div/input"));

EDIT: You should try XPATH like this:编辑:您应该像这样尝试 XPATH :

IWebElement XPathSpan = driver.FindElement(By.XPath("descendant::span[@class='spinner-placeholder' and text()='Price']")); //verify it found the span
IWebElement XPathInput = driver.FindElement(By.XPath("following-sibling::input")); //this should be your input

The XPATH you have is fine but if the dom changes so that there's an extra div in the hierarchy it's going to break the one you have.您拥有的 XPATH 很好,但如果 dom 发生更改,从而导致层次结构中有一个额外的 div,它将破坏您拥有的那个。 The key parts here is descendant tell it to traverse down through the children, grandchildren, and so on.这里的关键部分是后代告诉它向下遍历子、孙等。 Since you're using driver.FindElement() it's going to start at the root of the document.由于您使用的是 driver.FindElement() 它将从文档的根目录开始。 The second line we start from the span and tell it to find the first sibling that is of type input which should be the one you're looking for.第二行我们从跨度开始,告诉它找到第一个输入类型的兄弟,它应该是你正在寻找的那个。

Well first, I'd try finding it with XPATH: "descendant::span[text() = 'PRICE']/following-sibling::input"首先,我会尝试使用 XPATH 找到它:“descendant::span[text() = 'PRICE']/following-sibling::input"

That would seem to me to be more reliable, in case they add another input or something.在我看来,这似乎更可靠,以防他们添加其他输入或其他内容。

Given that it's parent has the class "spinner _focusable" and some javascript for onkeydown, I'd assume you'd find that element and interact with it first, then you can interact with the price one.鉴于它的父级具有 class “spinner _focusable”和一些用于 onkeydown 的 javascript,我假设您会先找到该元素并与之交互,然后您可以与价格交互。

So your steps would be:所以你的步骤是:

  1. find input with XPATH above用上面的 XPATH 查找输入
  2. find parent input such as var inputParent = input.FindElement(By.XPath("parent::div")) //assuming input is the name of the variable from step 1查找父输入,例如 var inputParent = input.FindElement(By.XPath("parent::div")) //假设输入是第 1 步中的变量名称
  3. inputParent.Click(); inputParent.Click();
  4. Thread.Sleep(500);线程.睡眠(500); //something like that to wait for the javascript, you could also use a webdriverwait as well //类似的东西等待javascript,你也可以使用webdriverwait
  5. input.SendKeys(...);输入.SendKeys(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM