我正在尝试通过部分类查找此元素,HTML代码如下:
<a class="follow-button profile" href="https://twitter.com/123"
role="button" data-scribe="component:followbutton" title="Follow 123
on Twitter"><i class="ic-button-bird"></i>Follow</a>
我使用的方法如下:
var element = Driver.FindElement(By.XPath("//a[contains(@class, 'follow-button profile')]"));
有什么想法可能是找不到元素的原因是什么?
NoSuchElement
异常通常具有以下原因之一:
您的元素在<frame>
或内<iframe>
。检查您的HTML。
SwitchTo().Frame()
Frame()
接受3个论点
IWebElement
IWebElement frameElement = driver.FindElement(By.Tag("frame"));
driver.SwitchTo().Frame(frameElement);
<frame name="MyFrame">
或的名称<iframe>
driver.SwitchTo().Frame("MyFrame");
<frame>
或的索引<iframe>
,从0开始
SwitchTo().Frame(0)
<frame>
上下文中完成操作后,您将需要切换回正确的上下文
driver.SwitchTo().DefaultContent();
在Selenium认为您的页面已完成加载后,您的元素正在页面上加载
Wait
用来告诉Selenium暂停,直到元素完成在页面上的加载。public WebDriverWait wait =新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(2)); wait.Until(驱动程序=> driver.FindElement(ByLocator));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句