【问题标题】:Uploading files using Selenium in C#?在C#中使用selenium上传文件?
【发布时间】:2018-03-31 13:28:46
【问题描述】:

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

以下是检查 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>

下面是我的 NUnit 类,其中传入了 XPath

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();
}

【问题讨论】:

  • 三个简单的问题:你的xpath在点击的那一刻有效吗?按钮是否在 iframe 标签内?你能暂停你的运行方法并检查浏览器控制台是否抛出了一些错误吗?
  • 它没有,达到我的断点@StriterAlfa
  • 您可以在 chrome 控制台上使用命令 $x("your xpath") 检查您的 xpath 是否有效。您还可以检查它是否在检查页面 html 的 iframe 内。如果调试器无法到达您的断点来调试您的单击操作,则说明有问题不是您寻求帮助的原因
  • @StriterAlfa 我得到的错误是 wait.PathWaitAndClick(driver, "*[@id=\"accordiongroup-299-451-panel\"]/div/div[2]/div[1 ]/table/tfoot/tr/td/button", 2000) 函数评估被禁用,因为之前的函数评估超时。您必须继续执行才能重新启用函数评估。无效
  • 我知道错误出现在该行,但是您是否按照我所说的测试了 xpath?您是否检查过该元素是否在 iframe 内?浏览器控制台有错误?所有这些点都会影响并导致该行出现错误。

标签: c# selenium xpath


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2016-08-29
    • 2021-02-08
    相关资源
    最近更新 更多