0

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. Can anyone help me?

I am using Chrome Browser for this.

enter image description here

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:

<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.

I also checked if my XPath is right and it showed correct. enter image description here

user1413
  • 527
  • 4
  • 21

2 Answers2

0

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:

  • 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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Nope that didn't work at all. I am able to login into the site and navigate to inbox but after that somehow it is failing to catch any element. – user1413 Jan 21 '19 at 17:46
  • Wait(WebDriverWait) is not a solution. Once page is loaded selenium is not able to catch any element from the page and throws posted error. – user1413 Jan 21 '19 at 22:04
0

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.

user1413
  • 527
  • 4
  • 21