简体   繁体   中英

Can't find this element on page even though it is exist C# selenium

I have tried finding this element for the longest time and I can't figure it out. My goal is too set the value to a number using setattribute but I can't find the element.

<input type="tel" id="cvNumber" tabindex="1" data-shortname="cvv" class="cc-input ncss-input pt2-sm pr4-sm pb2-sm pl4-sm u-align-center" "cvv"="" autocomplete="off" autocorrect="off" value="" maxlength="4">

Here is some code I used to try to find the element

driver.FindElement(By.XPath("//input[@class='cc-input ncss-input pt2-sm pr4-sm pb2-sm pl4-sm u-align-center');
driver.FindElement(By.Id("cvNumber");
driver.FindElement(By.XPath("//input[@id='cvNumber'");
driver.FindElement(By.className("cc-input ncss-input pt2-sm pr4-sm pb2-sm pl4-sm u-align-center");
driver.FindElement(By.XPath("//input[@type='tel'");

Thanks in advance

尝试使用此代码:

driver.FindElement(By.XPath("//input[@id='cvNumber'][@type='tel']");

I don't think there is anything wrong with the element selector. Its likely that you are not waiting for the element long enough

 var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
 var myElement = wait.Until(x => x.FindElement(By.Id("cvNumber")));
 if(myElement.Displayed) {
   //do stuff
 }

Or its possible that the element is inside a frame, so you need to switch to the frame before executing code above.

driver.SwitchTo().Frame(driver.FindElement(By.id("frameId")));

As per your shared HTML, you can use below location strategies

Xpath

.//*[@id='cvNumber']

CSS selector

#cvNumber

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