【问题标题】:How can i select the div id in Selenium 2 WebDriver?如何在 Selenium 2 WebDriver 中选择 div id?
【发布时间】:2016-04-01 20:40:12
【问题描述】:

我想用 C# 在 selenium 2 中找到一个标签,但我无法使用 id 或 xpath 等访问 div 标签。这是我的代码:

driver.FindElement(By.Id("captcha"));

网络来源是:

<body id="mainBody">
<div class="top_bar" style="min-height:210px">
    <div class="m_sidebar_view">
        <div id="captcha" style="position: relative; float: right; margin: 6px 35px 0 0; overflow: auto;"><div/>  
    </div>
</div>
</body>

【问题讨论】:

  • 试试这个 xpath: " //div[@class='top_bar"]/div/div "
  • 你用它做什么?

标签: c# html selenium xpath webdriver


【解决方案1】:

虽然不清楚您的问题是什么,但最常见的错误之一是没有等待元素。所以试试to wait:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
    return d.FindElement(By.Id("captcha"));
});

【讨论】:

  • 谢谢你的回答兄弟。但我确定我的问题不在于等待。 Web 驱动程序找不到具有此 ID 的元素。我不知道为什么
【解决方案2】:

验证码通常在iframe 中加载(例如,参见google recaptcha demo 源代码)。 在搜索验证码元素之前切换到它

driver.SwitchTo().Frame("frame_name_or_id");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    相关资源
    最近更新 更多