在C#中使用Selenium上传文件?

时间:2017-10-19 13:30:01

标签: c# selenium xpath

我正在尝试点击上传已完成的表单,使用XPath通过selenium,但是这对XPath没有任何作用,我想知道是否有人有任何建议的答案?

enter image description here

以下是检查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();
}

1 个答案:

答案 0 :(得分:0)

鉴于您提供的HTML背景有限,我会在您的XPath中使用contains来搜索包含字符串button的给定"Upload completed form"元素。

//button[contains(., 'Upload completed form')]