Sendkeys在selenium webdriver中不一致

本文关键字:不一致 webdriver selenium Sendkeys | 更新日期: 2023-09-27 18:04:40

我使用sendkeys上传文件,但由于某种原因,它非常不一致。我有两个场景来上传文件(在主窗口上附加一个文件,然后在第二个窗口上附加一个文件)。如果我使用第一种情况,即在主窗口上附加文件而不在第二个窗口上附加文件,则可以完美地工作。我也在第二个窗口上做了同样的事情,它工作得很好,但是当我同时运行时,sendkeys不会在我的确切字符串中键入。下面是代码:

AttachFileTA.cs:

  IWebElement fileAttachTA = driver.FindElement(By.XPath("//object[@class='ruObject']"));
fileAttachTA.Click();
         //Switch into the windows upload dialog
         Thread.Sleep(1000);
         SendKeys.SendWait(file);
         Thread.Sleep(1000);
         SendKeys.SendWait(@"{Enter}");
         Thread.Sleep(1000);
         SendKeys.Flush();

c:

 createDraftTA.FillGeneralInfo("This is a fourth positive test created from Selenium Webdriver");
 addPrepaid.AddPrePaid("test", "Comment");

AddPrepaid.cs:

  commentsBox.SendKeys("COMMENT");        
  CreateTANonProxy_Domestic attachFile = new CreateTANonProxy_Domestic(driver);
  attachFile.AttachFileTA("''Users''My Documents''file.txt");

FillGeneralInfo.cs

purposeBox.SendKeys(purpose);
AttachFileTA("''Users''My Documents''file.txt");

上传文件的最佳方式是什么?

谢谢

Sendkeys在selenium webdriver中不一致

首先,看起来你误解了SendKeys。请注意,在代码中SendKeys是c#类,而不是Selenium方法。

关于' SendKeys'类的更多信息

SendKeys (c#)类是不可靠的处理基于窗口的弹出时,你使用它在硒。代替它,尝试使用其他替代文件上传,例如AutoIt工具。在此基础上,尝试找出AutoIt的工作方式并实现解决方案来处理Selenium WebDriver中的文件上传。