Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Selenium中导入文件_C#_Selenium_Selenium Webdriver_Xpath_Css Selectors - Fatal编程技术网

C# 在Selenium中导入文件

C# 在Selenium中导入文件,c#,selenium,selenium-webdriver,xpath,css-selectors,C#,Selenium,Selenium Webdriver,Xpath,Css Selectors,页面包含此表单: <form target="PID297_TGT_FRAME" action="/app/upload/57aa897a64d9" class="form form-default" method="post" enctype="multipart/form-data"> <div> <input type="hidden"> <input name="PID297_file" class="fi

页面包含此
表单

<form target="PID297_TGT_FRAME" action="/app/upload/57aa897a64d9" class="form form-default" method="post" enctype="multipart/form-data">
    <div>
        <input type="hidden">
        <input name="PID297_file" class="file-upload" type="file">
        <div aria-pressed="false" role="button" class="v-button" tabindex="0">
            <span class="v-button-wrap">
                <span class="v-button-caption">Import</span>
            </span>
        </div>
    </div>
</form>
不幸的是,我收到
异常
消息:

Element is not currently visible and so may not be interacted with

我的代码有问题吗?

元素必须可见,selenium才能访问它。 如果有其他操作启用文件上载,请先在selenium代码中执行该操作。

此错误消息

Element is not currently visible and so may not be interacted with
…意味着WebDriver实例无法定位元素,因为它不可见

此消息的积极作用是所需元素在中存在,但不可见,可能是因为它不在中

解决方案 在调用
SendKeys()
之前,需要将所需元素放入视口中,如下所示:

  • css选择器

    new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.file-upload[name$='_file']"))).SendKeys(filePath);
    
  • XPath

    new WebDriverWait(Driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='file-upload' and contains(@name,'_file')]"))).SendKeys(filePath);
    

添加
Driver.RunScript(“jQuery(\“input\”).attr(\“visibility\”,\“visible\”);”)什么也没做。静止元素不可见。输入框对用户可见吗?它是因为某个类而隐藏的吗?另请参见此问题-可见不取决于元素是否在视口中。您可以查看源代码并自行查看。您是否尝试添加了等待可见?
new WebDriverWait(Driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='file-upload' and contains(@name,'_file')]"))).SendKeys(filePath);