简体   繁体   中英

Selenium WebDriver C#: Exception thrown finding text input element

<div class="_5yk2" tabindex="-1"><div class="_5rp7"><div class="_1p1t" style=""><div class="_1p1v" id="placeholder-7mj5h" style="white-space: pre-wrap;">Write a post...</div></div><div class="_5rpb"><div aria-autocomplete="list" aria-controls="js_fe" aria-describedby="placeholder-7mj5h" aria-label="Write a post..." aria-multiline="true" class="notranslate _5rpu" contenteditable="true" role="textbox" spellcheck="true" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;"><div data-contents="true"><div class="" data-block="true" data-editor="7mj5h" data-offset-key="6o271-0-0"><div data-offset-key="6o271-0-0" class="_1mf _1mj"><span data-offset-key="6o271-0-0"><br data-text="true"></span></div></div></div></div></div></div></div>

I'm using XPath in the Chrome Driver to Find the Text Area. Here is my code line.

ele = driver.FindElement(By.XPath("//div[@aria-autocomplete='list']"));

The Chrome Driver throws an NoSuchElementException. How can I get this element so I can send text to it?

To find the text input WebElement you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies :

  • CssSelector :

     IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div[aria-autocomplete='list'][aria-describedby^='placeholder'][aria-label='Write a post...']")));
  • XPath :

     IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@aria-autocomplete='list' and starts-with(@aria-describedby, 'placeholder')][@aria-label='Write a post...']")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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