在C#中使用Selenium上传文件?
问题描述:
我想点击按钮上传完成的表单,通过硒使用XPath,但是这不会做任何事情与它的XPath,我想知道如果有人有任何建议的答案?在C#中使用Selenium上传文件?
下面是检查Chrome浏览器的HTML。
<button ng-if="$root.AccessLevel.Standard" class="btn btn--small btn--icon ng-pristine ng-valid ng-touched" ng-model="vm.uploads[workCategory.Id].reference" ngf-max-size="'5MB'" ngf-model-invalid="vm.uploads[workCategory.Id].referenceInvalid" ngf-pattern="'.pdf,.doc,.docx,.bmp,.gif,.jpg,.jpeg,.tif,.wmf,.png'" ngf-select="vm.uploadFile($file, 'WorkCategoryReference', workCategory.Id, workCategory.References, workCategory.Id+'uploadRef')">
<span promise="vm.promises[workCategory.Id+'uploadRef']">
<span ng-hide="pending" class="">
<i class="fa fa-lg fa-cloud-upload"></i>
</span>
<span ng-show="pending" class="ng-hide">
<i class="fa fa-lg fa-refresh fa-spin"></i>
</span>
</span>
Upload completed form
</button>
下面是我用的XPath的NUnit类
wait.PathWaitAndClick(driver, "*[@id=\"accordiongroup - 299 - 4516 - panel\"]/div/div[2]/div[1]/table/tfoot/tr/td/button", 2000);
通过以下还我使用WebExtension方法。
public static void PathWaitAndClick(this WebDriverWait wait, IWebDriver driver, string path, int pause = 0)
{
if (pause > 0)
{
System.Threading.Thread.Sleep(pause);
}
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(path)));
IWebElement viewAllWc = driver.FindElement(By.XPath(path));
if (pause > 0)
{
System.Threading.Thread.Sleep(pause);
}
viewAllWc.Click();
}
答
鉴于您提供HTML的有限范围内,我将利用contains
在你的XPath搜索包含字符串"Upload completed form"
给出button
元素。
//button[contains(., 'Upload completed form')]
三个简单的问题:您的xpath是否在点击时刻有效? iframe标签中的按钮是?你可以暂停你正在运行的方法,并检查是否有一些错误在浏览器控制台丢失? –
它没有,打我的折点@StriterAlfa –
你可以检查你的xpath是否有效使用命令$ x(“你的xpath”)在铬控制台。你也可以检查它是否在iframe中检查页面的html。如果调试程序无法到达断点来调试您的单击操作,则出现错误,这不是您要求的帮助 –