简体   繁体   English

即使存在C#硒,也无法在页面上找到此元素

[英]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.

相关问题 为什么 Selenium 无法读取下拉文本,即使它们存在于 HTML 中。 (C#, VS2019, 硒) - Why Selenium can't read texts of dropdown even though those exist in HTML. (C#, VS2019, Selenium) 使用Pagefactory在页面上存在硒c#查找元素 - Selenium c# Find element exist on a page using Pagefactory 无法使用硒C#在网页中找到元素? - Can't find element in webpage using selenium C#? 硒-driver.find_element_by_css_selector找不到元素(C#) - Selenium - driver.find_element_by_css_selector can't find the element (c#) Selenium WebDriver C#:即使元素被禁用,Element.Enabled 也会返回 True - Selenium WebDriver C#: Element.Enabled returns True even though element is disabled C#即使继承,也无法转换为接口 - C# Can't cast to interface even though it inherits it C#Selenium找不到按钮 - C# Selenium can't find button C#Selenium无法通过WebDriverWait找到元素,我可以手动查看并单击它 - C# Selenium can't find element with WebDriverWait, i can see and click on it manually Selenium C# 错误:即使它是 DOM 可点击的 ElementNotIteractableException - Selenium C# error: ElementNotIteractableException even though it is DOM clickable 即使目录存在,我的C#代码也无法在C驱动器中看到我的目录。 - My C# code can't see my directory in my C drive…even though the directory exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM