【问题标题】:Selenium not able to input element in the form inside fieldset tagSelenium 无法在字段集标记内的表单中输入元素
【发布时间】:2021-12-10 09:44:11
【问题描述】:

我尝试了一种不同的方法来输入元素,例如 这是我使用的 xpath

By NameOfTheProperty=By.xpath("//fieldset/div/input[@name='name']");
By NameOfTheProperty=By.xpath("//div/input[@name='name']");
By NameOfTheProperty=By.xpath("//input[@name='name']");

已尝试使用 Selenium Builder

 WebElement element=driver.findElement(by);
        Actions builder = new Actions(driver);
        Action mouseOverHome = builder
                .moveToElement(element)
                .click().sendKeys(text).build();
        mouseOverHome.perform();

试过

 WebElement element=driver.findElement(by);
        element.sendKeys(text);

这些方法都不起作用..我无法在字段内输入文本并显示

元素不可交互

这里是网站

我希望有人帮我找出解决方案..

【问题讨论】:

    标签: java selenium xpath


    【解决方案1】:

    如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

    检查步骤:

    Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654328@匹配节点。

    你应该检查的xpath是:

    //input[@name='name']
    

    如果它是唯一的,那么你可以使用 Javascript 执行器:

    WebElement password_input = driver.findElemenet(By.xpath("//input[@name='name']"));
    ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', 'password_should_be_written_here')", password_input)
    

    【讨论】:

    • 感谢您的回答,Xpath 是正确的,但不幸的是无法正常工作。我认为由于字段集 XPath 无法导航该字段。一些答案我发现他们在 XPath 中使用了祖先,但不明白如何使用它!你认为呢?我不知道如何使用祖先
    • 我不认为我们应该使用祖先。 XPath 中的祖先是从当前节点到祖先节点。你可以这样做//input[@name='name']/ancestor::fieldset 。话虽如此,我们希望专注于输入字段。
    • 您还应该检查此输入标签是否在任何 iframe 中。或在影子根下。你真的确定我们是否有正确的 xpath 吗?如果正确,它在 UI 中的什么位置?
    【解决方案2】:

    如果元素有一个,我个人会使用 id。

    "//*[@id='__BVID__104']"
    

    确保您正在等待元素准备就绪。

    WebDriverWait wait = new WebDriverWait();
    wait.until(ExpectedConditions.elementToBeClickable(element));
    

    如果它在 wait.until(... 上超时,那么我发现有时必须先单击一个元素才能使其暴露。

    如果是这样的话。您必须在单击该元素之前对其进行检查。找到它的xpath,看看点击的时候有没有变化。如果是这样,则为该元素创建一个 webElement 并让 Selenium 先单击它,然后再单击实际的字段/元素。

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 2010-10-29
      • 1970-01-01
      • 2016-01-07
      相关资源
      最近更新 更多