【问题标题】:Need help to click on the element under the shadow Root (user-agent) type需要帮助点击shadow Root(user-agent)类型下的元素
【发布时间】:2021-09-06 01:45:21
【问题描述】:

由于我没有处理 shadow-root 元素的经验,我无法点击这个名为“Choose File”的按钮

我正在尝试单击 shadow-root (user-agent) 元素下的“选择文件”按钮,我可以获得代码方面的帮助吗?

下面的HTML代码::

<input class="webbrowse" id="web263">
  #shadow-root (user-agent)
    <input type="button" value="Choose File">
      #shadow-root (user-agent)
        "Choose File"
      </input>
      <span aria-hidden="true">No file chosen</span>
</input>

谢谢大家,我真的很感激。

【问题讨论】:

  • 您尝试过任何代码吗?你有什么错误吗?
  • 我试过了,不行,没有点击按钮
  • cutt.ly/Qn3RjW3 看看这个

标签: javascript java selenium webdriver testng


【解决方案1】:

利用JavascriptExecutor

WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", driver.findElement(By.xpath("//input[value = 'Choose File']")));
element.sendKeys("some keys");

【讨论】:

  • 此代码无效。例外:没有这样的元素 xpath 选择器://input[value='Choose File']
【解决方案2】:

您必须访问父节点 (&lt;input class="webbrowse" id="web263"&gt;),然后才能找到您的输入:

WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return document.querySelector('.webbrowse').shadowRoot.querySelector('input[value=\"Choose File\"]');");

element.click();

【讨论】:

    猜你喜欢
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多