简体   繁体   中英

No such element: Unable to locate element OpenQA.Selenium in C#

I am facing this exception:

OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#MuliCloudAccountSelector"}

My Code:

driver.FindElement(By.Id("MuliCloudAccountSelector")).Click();

My HTML:

<div id="MuliCloudAccountSelector" class="antd-pro-src-components-page-header-wrapper-multi-account-selector-antpopover" style="border: 1px solid rgb(217, 217, 217); margin-left: 6px; margin-right: 10px; height: 40px; float: left; min-width: 24px; border-radius: 4px; max-width: 316px; overflow: hidden;">

What am I missing?

You need to wait for the element to be clickable:

var wait = new WebDriverWait(drive, TimeSpan.FromSeconds(30));
var div = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("MuliCloudAccountSelector"));

div.Click();

There is likely a race condition occurring between Selenium and the browser. You are finding the element before the element is rendered in the page.

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