如何在WebDriver中获取元素的父级

use*_*124 0 selenium-webdriver

<div class="field select-container invalid" data-tooltip="Error message." "name="birthDate">
<div class="stack-wrap gutter-col-wrap-1">
<div class="stack size1of3">
<div class="gutter-col-1">
    <div class="form-selectbox full-width">
    <select name="day">
        <option value="">Giorno</option>
        <option value="01">1</option>
Run Code Online (Sandbox Code Playgroud)

使用WebElement el = browser.switchTo()。activeElement()时,焦点位于“名称”属性上。需要找到“ data-tooltip”的父项才能在屏幕上获取错误消息。请帮忙。

小智 6

为了仅查找子元素的父代,可以使用以下代码。

IWebElement parentElement = childElement.FindElement(By.XPath(".."));
Run Code Online (Sandbox Code Playgroud)