硒|无法定位输入元素
不知道如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要将字符序列发送到元素,您需要为elementToBeClickable(),您可以使用以下任一方法定位策略:
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:
Xpath:
请检查
开发工具
(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')]
那么您还需要检查以下条件。检查它是否在任何
iframe/frame/frameset
中。解决方案:先切换到 iframe/frame/frameset,然后与此 Web 元素交互。
检查它是否在任何
shadow-root
中。解决方案:使用
driver.execute_script('return document.querySelector
)返回一个Web元素,然后进行相应的操作。在交互之前确保该元素正确渲染加上一些
硬编码延迟
或显式等待
,然后重试。解决方案:
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("测试")
如果您已重定向到
新选项卡/或新窗口
并且您尚未切换到特定的新选项卡/新窗口
,否则您可能会遇到NoSuchElement
异常。解决方案:先切换到相关窗口/选项卡。
如果您已切换到 iframe 并且新的所需元素不在同一 iframe 上下文中,则首先
切换到默认内容
,然后与其交互。解决方案:切换到默认内容,然后切换到相应的 iframe。
可以从step1开始调试。
更新:
特定问题的解决方案:
在
在此处粘贴查询选择器
的位置,您必须按 F12 再次转到 goog chrome 中的开发工具,然后它会是这样的:
用
""
中包装的内容替换此在此处粘贴查询选择器
Xpath:
Please check in the
dev tools
(Google chrome) if we have unique entry inHTML DOM
or not.Steps to check:
Press F12 in Chrome
-> go toelement
section -> do aCTRL + F
-> then paste thexpath
and see, if your desiredelement
is getting highlighted with1/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.Check if it's in any
iframe/frame/frameset
.Solution: switch to iframe/frame/frameset first and then interact with this web element.
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.Make sure that the element is rendered properly before interacting with it. Put some
hardcoded delay
orExplicit wait
and try again.Solution:
time.sleep(5)
orWebDriverWait(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")
If you have redirected to a
new tab/ or new windows
and you have not switched to that particularnew tab/new window
, otherwise you will likely getNoSuchElement
exception.Solution: switch to the relevant window/tab first.
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:
in the place of
paste query selector here
you will have to go to dev tools again in goog chrome by pressing F12, and thenIt'd be something like this:
replace this
paste query selector here
with the stuff that is wrapped inside""
根据提供的 html,这有效:
driver.find_element(By.XPATH, "//input[@part='value' and contains(@aria-labelledby, 'vaadin-text-field-input')]" ]
输出:
Per the html provided, this works:
driver.find_element(By.XPATH, "//input[@part='value' and contains(@aria-labelledby, 'vaadin-text-field-input')]"]
Output:
看起来 HTML 的一部分如下所示:
更好的是从某个 id 字段构建 XPATH 定位器:
另外,如前所述必须检查:
input
元素不是动态的,则如果您发现该注释已解决,您可以移至代码部分并使用 WebDriver 建立的定位器:
另外,请记住你必须知道那个元素正如之前所建议的,您可以使用一些 显式等待 为您的元素。
Looks like part of HTML looks like:
Much better is to build your XPATH locator from some id field:
Also, as mentioned earlier have to check:
input
element is not dynamicIf you found solved that comment you could move to the code part and use the founded locator with WebDriver:
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.