硒|无法定位输入元素

发布于 2025-01-14 12:02:29 字数 738 浏览 1 评论 0原文

不知道如何使用 selenium / Java 来处理此输入文本字段元素(openjdk 11 2018-09-25)。

我尝试了 xpath、cssSelector 等,但它从来没有工作过。它总是“无法找到元素”。

<slot name="input">
   <input part="value" tabindex="0" aria-labelledby="vaadin-text-field-input-3">
</slot>

这不起作用:

driver.findElement(By.xpath("//input[@aria-labelledby='vaadin-text-field-input-3']")).sendKeys("test");

有解决方案吗?

更新:

该线程部分解决了我的问题。对我有用的解决方案可以在此处找到链接

No idea how to address this input text field element with selenium / Java (openjdk 11 2018-09-25).

I tried xpath, cssSelector etc. it never works. Its always "Unable to locate element".

<slot name="input">
   <input part="value" tabindex="0" aria-labelledby="vaadin-text-field-input-3">
</slot>

This did NOT work:

driver.findElement(By.xpath("//input[@aria-labelledby='vaadin-text-field-input-3']")).sendKeys("test");

Is there a solution for this?

UPDATE:

This thread solved partly my problem. The solution that worked for me can be found here link.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疯到世界奔溃 2025-01-21 12:02:29

要将字符序列发送到元素,您需要为elementToBeClickable(),您可以使用以下任一方法定位策略

new WebDriverWait(driver, 20)
    .until(ExpectedConditions.elementToBeClickable(
          By.cssSelector("div.vaadin-text-field-container div[part=input-field][id^='vaadin-text-field-input'] slot[name='input'] > input[part='value'][aria-labelledby^='vaadin-text-field-input']")
    )).sendKeys("pixelhead");
new WebDriverWait(driver, 20)
   .until(ExpectedConditions.elementToBeClickable(
      By.xpath("//div[@class='vaadin-text-field-container']//div[@part='input-field' and starts-with(@id, 'vaadin-text-field-input')]//slot[@name='input']/input[@part='value' and starts-with(@aria-labelledby, 'vaadin-text-field-input')]")
   )).sendKeys("pixelhead");

To send a character sequence to the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

new WebDriverWait(driver, 20)
    .until(ExpectedConditions.elementToBeClickable(
          By.cssSelector("div.vaadin-text-field-container div[part=input-field][id^='vaadin-text-field-input'] slot[name='input'] > input[part='value'][aria-labelledby^='vaadin-text-field-input']")
    )).sendKeys("pixelhead");
new WebDriverWait(driver, 20)
   .until(ExpectedConditions.elementToBeClickable(
      By.xpath("//div[@class='vaadin-text-field-container']//div[@part='input-field' and starts-with(@id, 'vaadin-text-field-input')]//slot[@name='input']/input[@part='value' and starts-with(@aria-labelledby, 'vaadin-text-field-input')]")
   )).sendKeys("pixelhead");
摇划花蜜的午后 2025-01-21 12:02:29

Xpath:

//div[@class='vaadin-text-field-container']//descendant::input[@part='value' and starts-with(@aria-labelledby, 'vaadin-text-field-input')]

请检查开发工具(Google chrome)我们在HTML DOM中是否有唯一条目。

检查步骤:

在 Chrome 中按 F12 ->转到 element 部分 ->执行 CTRL + F ->然后粘贴xpath并查看您所需的元素是否通过1/1匹配节点突出显示

如果这是唯一的 //div[@class='vaadin-text-field-container']//descendant::input[@part='value' andstarts-with(@aria-labelledby, 'vaadin- text-field-input')] 那么您还需要检查以下条件。

  1. 检查它是否在任何 iframe/frame/frameset 中。

    解决方案:先切换到 iframe/frame/frameset,然后与此 Web 元素交互。

  2. 检查它是否在任何 shadow-root 中。

    解决方案:使用driver.execute_script('return document.querySelector)返回一个Web元素,然后进行相应的操作。

  3. 在交互之前确保该元素正确渲染加上一些硬编码延迟显式等待,然后重试。

    解决方案: time.sleep(5)

    WebDriverWait(driver, 20).until(EC.visibility_of_element_ located((By.XPATH, "//div[@class='vaadin-text-field-container']//descendant::input[@部分='值'并开始于(@aria-labelledby, 'vaadin-text-field-input')]"))).send_keys("测试")

  4. 如果您已重定向到新选项卡/或新窗口并且您尚未切换到特定的新选项卡/新窗口,否则您可能会遇到NoSuchElement异常。

    解决方案:先切换到相关窗口/选项卡。

  5. 如果您已切换到 iframe 并且新的所需元素不在同一 iframe 上下文中,则首先切换到默认内容,然后与其交互。

    解决方案:切换到默认内容,然后切换到相应的 iframe。

可以从step1开始调试。

更新:

特定问题的解决方案:

Thread.sleep(2000);
WebElement inputButton = (WebElement) ((JavascriptExecutor)driver).executeScript("return document.querySelector('#TextFieldTitle').shadowRoot.querySelector('#vaadin-text-field-input-3 > slot:nth-child(2) > input')");
inputButton.sendKeys("test");

在此处粘贴查询选择器的位置,您必须按 F12 再次转到 goog chrome 中的开发工具,然后

  1. 转到那个输入框
  2. 右键
  3. 选择复制
  4. 选择复制JS路径。
  5. 按 Ctrl + v 进入记事本以查看从开发工具中获得的内容。

它会是这样的:

document.querySelector("#vaadin-text-field-input-3 > slot:nth-child(2) > input")

"" 中包装的内容替换此 在此处粘贴查询选择器

Xpath:

//div[@class='vaadin-text-field-container']//descendant::input[@part='value' and starts-with(@aria-labelledby, 'vaadin-text-field-input')]

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

If this is unique //div[@class='vaadin-text-field-container']//descendant::input[@part='value' and starts-with(@aria-labelledby, 'vaadin-text-field-input')] then you need to check for the below conditions as well.

  1. Check if it's in any iframe/frame/frameset.

    Solution: switch to iframe/frame/frameset first and then interact with this web element.

  2. Check if it's in any shadow-root.

    Solution: Use driver.execute_script('return document.querySelector to have returned a web element and then operates accordingly.

  3. Make sure that the element is rendered properly before interacting with it. Put some hardcoded delay or Explicit wait and try again.

    Solution: time.sleep(5) or

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='vaadin-text-field-container']//descendant::input[@part='value' and starts-with(@aria-labelledby, 'vaadin-text-field-input')]"))).send_keys("test")

  4. If you have redirected to a new tab/ or new windows and you have not switched to that particular new tab/new window, otherwise you will likely get NoSuchElement exception.

    Solution: switch to the relevant window/tab first.

  5. If you have switched to an iframe and the new desired element is not in the same iframe context then first switch to default content and then interact with it.

    Solution: switch to default content and then switch to respective iframe.

You can start debugging from step1.

Update:

Solution specific to the problem:

Thread.sleep(2000);
WebElement inputButton = (WebElement) ((JavascriptExecutor)driver).executeScript("return document.querySelector('#TextFieldTitle').shadowRoot.querySelector('#vaadin-text-field-input-3 > slot:nth-child(2) > input')");
inputButton.sendKeys("test");

in the place of paste query selector here you will have to go to dev tools again in goog chrome by pressing F12, and then

  1. Go to that input box
  2. Do a right click
  3. Select copy
  4. Select copy JS path.
  5. Ctrl + v into notepad to see what you've got from the dev tool.

It'd be something like this:

document.querySelector("#vaadin-text-field-input-3 > slot:nth-child(2) > input")

replace this paste query selector here with the stuff that is wrapped inside ""

等风来 2025-01-21 12:02:29

根据提供的 html,这有效:

driver.find_element(By.XPATH, "//input[@part='value' and contains(@aria-labelledby, 'vaadin-text-field-input')]" ]

x = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@part='value' and contains(@aria-labelledby, 'vaadin-text-field-input')]")))
x.send_keys('This is typed here by selenium')
print(f"the typed text in input box is: {x.get_attribute('value')}")

输出:

the typed text in input box is: This is typed here by selenium

Process finished with exit code 0

Per the html provided, this works:

driver.find_element(By.XPATH, "//input[@part='value' and contains(@aria-labelledby, 'vaadin-text-field-input')]"]

x = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@part='value' and contains(@aria-labelledby, 'vaadin-text-field-input')]")))
x.send_keys('This is typed here by selenium')
print(f"the typed text in input box is: {x.get_attribute('value')}")

Output:

the typed text in input box is: This is typed here by selenium

Process finished with exit code 0
猫弦 2025-01-21 12:02:29

看起来 HTML 的一部分如下所示:

<div class="vaadin-text-field-container">
    <label part="label" id="vaadin-text-field-label-3"></label>
    <div part="input-field" id="vaadin-text-field-input-3">
        <slot name="prefix"></slot>
        <slot name="input">
            <input part="value" tabindex="0" aria-labelledby="vaadin-text-field-input-3">
        </slot>
    </div>
</div>

更好的是从某个 id 字段构建 XPATH 定位器:

//div[@id='vaadin-text-field-input-3']//input[contains(@aria-labelledby, 'vaadin-text-field-input')]"]

另外,如前所述必须检查:

  • 该页面是否唯一
  • 如果input 元素不是动态的,则

如果您发现该注释已解决,您可以移至代码部分并使用 WebDriver 建立的定位器:

driver.findElement(
     By.xpath("//div[@id='vaadin-text-field-input-3']//input[contains(@aria-labelledby, 'vaadin-text-field-input-3')]"]"
)).sendKeys("test");

另外,请记住你必须知道那个元素正如之前所建议的,您可以使用一些 显式等待 为您的元素。

Looks like part of HTML looks like:

<div class="vaadin-text-field-container">
    <label part="label" id="vaadin-text-field-label-3"></label>
    <div part="input-field" id="vaadin-text-field-input-3">
        <slot name="prefix"></slot>
        <slot name="input">
            <input part="value" tabindex="0" aria-labelledby="vaadin-text-field-input-3">
        </slot>
    </div>
</div>

Much better is to build your XPATH locator from some id field:

//div[@id='vaadin-text-field-input-3']//input[contains(@aria-labelledby, 'vaadin-text-field-input')]"]

Also, as mentioned earlier have to check:

  • if it is unique for this page
  • if the input element is not dynamic

If you found solved that comment you could move to the code part and use the founded locator with WebDriver:

driver.findElement(
     By.xpath("//div[@id='vaadin-text-field-input-3']//input[contains(@aria-labelledby, 'vaadin-text-field-input-3')]"]"
)).sendKeys("test");

Also, keep in mind that you have to know that element is already loaded on a page. As suggested before you could use some explicit wait for your element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文