[英]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. 我的目标也是使用setattribute将值设置为数字,但是我找不到该元素。
<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 根据您共享的HTML,您可以使用以下定位策略
Xpath Xpath
.//*[@id='cvNumber']
CSS selector CSS选择器
#cvNumber
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.