简体   繁体   English

如何单击Office365 Hotmail搜索输入框

[英]How to click on Office365 Hotmail search input box

I have created an automation to open an email via searching email address. 我创建了一种自动化功能,可以通过搜索电子邮件地址来打开电子邮件。 But somehow I am unable to click on the search box. 但是我无法以某种方式单击搜索框。 I tried using various XPath as well as using Action both failed. 我尝试使用各种XPath以及使用Action均失败。 Can anyone help me? 谁能帮我?

I am using Chrome Browser for this. 我正在为此使用Chrome浏览器。

在此处输入图片说明

Using Action 使用动作

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

Using Element Click 使用元素点击

private static string SearchIcon = "//*[contains(@aria-label,'Activate Search Textbox')]";
ElementClick(Driver.FindElement(By.XPath(SearchIcon)));

Relevant HTML: 相关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>

Error: 错误:

The HTTP request to the remote WebDriver server for URL http://localhost:.../session/c9ac8d163f26dd172417d63f33a65373/element timed out after 60 seconds. 到远程WebDriver服务器的URL http:// localhost:... / session / c9ac8d163f26dd172417d63f33a65373 / element的HTTP请求在60秒后超时。

I also checked if my XPath is right and it showed correct. 我还检查了我的XPath是否正确,并且显示正确。 在此处输入图片说明

The desired element looks to be a dynamic element so you have to induce WebDriverWait for the desired ElementToBeClickable and you can use either of the following Locator Strategies as solutions: 所需的元素看起来是动态元素,因此您必须为所需的ElementToBeClickable诱导WebDriverWait ,并且可以使用以下定位策略之一作为解决方案:

  • CssSelector : 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 : 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(); 

Solution to my question 解决我的问题

IWebElement SearchElement = Driver.FindElement(By.XPath("//button[@aria-label='Activate Search Textbox']"));
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript("arguments[0].click();", SearchElement);

Somehow using below script was not catching the element by selenium driver. 以某种方式使用以下脚本无法通过硒驱动程序捕获元素。

private static string SearchIcon = "//button[@aria-label='Activate Search Textbox']";
ElementClick(Driver.FindElement(By.XPath(SearchIcon)));

Marking this solution post as solved to help other viewer incase if they need as a reference for their script. 将此解决方案帖子标记为已解决,以帮助其他查看者以防他们需要作为其脚本的参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM