我的网页包含文字,文字字段,普通表单按钮和文件上传按钮(打开文件上传对话框)。
除文件上传按钮外,可以访问所有其他元素。我已尝试使用.Click()和JavaScriptExecutor,但据我所见(视觉上),文件对话框永远不会打开。但是,没有错误消息。
页面来源:
<a class="bttngrey file-input-container bttn-small" data-bind="enable: !uploading() " style="margin-top: 10px; ... data-original-title="Add attachment">
<i class="fa fa-cloud-upload">...</i>
<input type="file" data-bind="upload: addAttachments, enable: !uploading()"> == $9
单击按钮的C#/ Selenium代码:
注意:我使用Button类和JavaScriptActions类来处理对ChromeDriver实例的调用,而不是直接调用它。我希望代码片段有意义。
Button.FindByXPath("/html/body/div[1]/div[2]/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a").Click();
JavaScriptActions.ButtonClickXPath("/html/body/div[1]/div[2]/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a");
public class Button {
public static IWebElement FindByXPath (string bttnxpath) {
return GCDriver.Instance.FindElement(By.XPath(bttnxpath));
}
}
public class JavascriptActions {
public static void ButtonClickXPath (string xpath) {
GCDriver.JSClickXPath(xpath);
}
}
public class GCDriver {
....
....
....
public static void JSClickXPath (string xpath) {
IWebElement icon = Instance.FindElement(By.XPath(xpath));
Actions ob = new Actions(Instance);
ob.Click(icon);
IAction action = ob.Build();
action.Perform();
}
....
....
....
}
点击按钮的方法似乎都不起作用。即使它们适用于其他正常的&#34;页面上的按钮。我尝试使用JavaScriptExecutor的原因是我之前遇到过像这样的按钮(打开文件对话框)不能被正常的Selenium方法点击的情况,但它是使用JavaScriptExecutor。
这里奇怪的是两种方法都完成而没有任何错误。但是,当我在观看页面时,我看不到对话框的单击和打开。测试刚刚结束。在单击按钮之前和之后添加显式等待也没有帮助。
有什么想法吗?如有必要,我会详细说明;并不总是清楚需要显示多少代码才能明确问题/问题。
答案 0 :(得分:1)
请在下面的代码中尝试使用我项目中的类似代码:
IWebElement element = new WebDriverWait(_browserWindow,TimeSpan.FromSeconds(20))。until(ExpectedConditions.ElementToBeClickable(By.CssSelector(&#34; [class =&#39; fa fa-cloud-upload&#39;] &#34;)));
//发送完整的文件路径以附加它
element.SendKeys(&#34;文件路径&#34;);
此外,根据发布的HTML代码,上传按钮包含复合类名称。因此,请参阅此复合类元素定位器: Compound class names are not supported. Consider searching for one class name and filtering the results
答案 1 :(得分:0)
试试这个:(使用驱动程序类实例)
Actions actions = new Actions(Driver.Instance);
actions.MoveToElement(Driver.Instance.FindElement(By.XPath(xpath)));
actions.Click();
actions.Build().Perform();
只需在驱动程序类中创建一个方法,然后从代码中的相关位置调用它。
你可以使用SendKeys发送文件路径,但我通常这样做的方法是使用AutoIt库来处理窗口上的操作,因为它不是浏览器/ web元素。