无法使用硒 C# 在网页中找到元素
本文关键字:网页 元素 | 更新日期: 2023-09-27 18:35:59
在
页面中找不到任何元素有 2 个嵌套在硒中的 iframe?
第一次启动时,我在浏览器中找到了所有元素,但是何时转到 iframe[0],我在浏览器中找不到任何元素。
我的代码是这样的:
var iframes = browser.FindElementsByXPathName("//iframe[contains(@id,'container_page')]");
while (browser.FindElementsByXPathName("//span[contains(@class, 'button_Love')]").Count == 0 && iframes.Count == 0)
Thread.Sleep(1000);
if (iframes.Count > 0)
{
browser.GoToFrame(iframes[0]);
Thread.Sleep(2000);
}
尝试以下代码。
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("you URL");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
//check if there is any iframe then get that frame and switch on frame using folloiwng code.
var frame = wait.Until(ExpectedConditions.ElementExists(By.Id("frame_id")));
driver.SwitchTo().Frame(frame);
var elementbutton_Love = driver.FindElements(By.ClassName("button_Love"));
//after doing all work you can go to default content using following code
driver.SwitchTo().DefaultContent();
如果有任何问题,请告诉我。