Chrome SendKeys()并非每次都通过Selenium和C#成功发送到输入元素

unftdfkk  于 2023-02-27  发布在  Go
关注(0)|答案(1)|浏览(176)

我使用 selenium (v4.0.30319)与 chrome 浏览器。已经尝试了一些设置,以确保结果总是成功。以下是最成功的结果。任何建议,以确保成功率更。99.99%:)
超文本:

<lw-text-input _nghost-qxr-c303="" class="ng-star-inserted lwCanvasWrapperComponent" tabindex="17" 
        style="transform: matrix(1, 0, 0, 1, 802.368, 241.776); visibility: visible; transform-origin: 0px 0px;">
        <textarea _ngcontent-qxr-c303="" name="PrimeInputTextArea" pinputtextarea="" lwngmodelcomponent="" tooltipposition="right" tooltipstyleclass="lwErrorTooltip" tooltipevent="focus" appendto="body" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" droppable="" rows="1" cols="6" class="ui-inputtext ui-corner-all ui-state-default ui-widget ng-untouched ng-pristine ng-valid ng-star-inserted" style="visibility: visible; width: 72px; font-family: Arial, Helvetica, sans-serif; font-size: 16px; font-weight: normal; font-style: normal; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
        </textarea>
        </lw-text-input>

<lw-text-input _nghost-qtr-c303="" class="ng-star-inserted lwCanvasWrapperComponent" tabindex="6" style="transform: matrix(1, 0, 0, 1, 781.776, 152.568); visibility: visible; transform-origin: 0px 0px;">
    <textarea _ngcontent-qtr-c303="" name="PrimeInputTextArea" pinputtextarea="" lwngmodelcomponent="" tooltipposition="right" tooltipstyleclass="lwErrorTooltip" tooltipevent="focus" appendto="body" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" droppable="" rows="1" cols="8" class="ui-inputtext ui-corner-all ui-state-default ui-widget ng-untouched ng-pristine ng-valid ng-star-inserted" style="visibility: visible; width: 169.92px; font-family: Arial, Helvetica, sans-serif; font-size: 16px; font-weight: normal; font-style: normal; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
            </textarea>
            </lw-text-input>

C编号:

WebDriverWait wait2 = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myField8 = wait2.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("lw-text-input[style='transform: matrix(1, 0, 0, 1, 802.368, 241.776); visibility: visible; transform-origin: 0px 0px;']")));

            if (myField8.Enabled)
            {
                SendKeysToElement(myField8, "AA25");
            }


 public void SendKeysToElement(IWebElement element, string text)
        {
            
            string bar = string.Empty;
            foreach (char c in text)
            {
                bar += c;
                System.Threading.Thread.Sleep(200);  // wait for 200ms between each character
            }

            System.Threading.Thread.Sleep(5000);
            element.Click();
            element.SendKeys(bar);

        }
uemypmqf

uemypmqf1#

看起来CssSelector有点不稳定。试试下面的选择器,看看这是否能提高代码的可靠性。

IWebElement myField8 = wait2.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("lw-text-input.lwCanvasWrapperComponent")));

IWebElement myField8 = wait2.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("lw-text-input.lwCanvasWrapperComponent > textarea[name='PrimeInputTextArea']")));

相关问题