我创建了一种自动化功能,可以通过搜索电子邮件地址打开电子邮件。但是我无法以某种方式单击搜索框。我尝试使用各种XPath以及使用Action均失败。谁能帮我吗?
我正在为此使用Chrome浏览器。
使用操作
Actions ob = new Actions(Driver);
ob.MoveToElement(Driver.FindElement(By.XPath("//*[contains(@aria-label,'Activate Search Textbox')]")));
ob.Click(Driver.FindElement(By.XPath("//*[contains(@aria-label,'Activate Search Textbox')]")));
Actions action = new Actions(Driver);
action.Perform();
使用元素点击
private static string SearchIcon = "//*[contains(@aria-label,'Activate Search Textbox')]";
ElementClick(Driver.FindElement(By.XPath(SearchIcon)));
相关HTML:
<button autoid="_n_4" type="button" class="_n_j ms-bgc-tl-h _n_k ms-bgc-tlr o365button ms-border-color-themeLighter" aria-label="Activate Search Textbox" style="">
<span class="_n_m owaimg ms-Icon--search ms-icon-font-size-20 ms-fcl-ts-b"> </span>
<span class="_n_l ms-fwt-sl ms-fcl-ns ms-fcl-np">Search Mail and People</span>
</button>
错误:
对远程WebDriver服务器的URL的HTTP请求 http://localhost:.../session/c9ac8d163f26dd172417d63f33a65373/element 60秒后超时。
答案 0 :(得分:0)
所需元素看起来是动态元素,因此您必须为所需的 ElementToBeClickable 诱导 WebDriverWait ,并且可以使用以下Locator Strategies中的任意一个作为解决方案:
CssSelector
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button.o365button[aria-label='Activate Search Textbox'] span:nth-child(2)"))).Click();
XPath
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[contains(@class, 'o365button') and @aria-label='Activate Search Textbox']//span[text()='Search Mail and People']"))).Click();
答案 1 :(得分:0)
我的问题的解决方案
IWebElement SearchElement = Driver.FindElement(By.XPath("//button[@aria-label='Activate Search Textbox']"));
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript("arguments[0].click();", SearchElement);
以某种方式使用以下脚本无法通过硒驱动程序捕获元素。
private static string SearchIcon = "//button[@aria-label='Activate Search Textbox']";
ElementClick(Driver.FindElement(By.XPath(SearchIcon)));
将此解决方案帖子标记为已解决,以帮助其他查看者以防他们需要作为其脚本的参考。