获取“Nosuch 元素异常"在 Selenium 中虽然 XPATH 是正确的.不确定这是不是由于 Shadow DOM.请确认 [英] Getting "Nosuch element Exception" in Selenium Though XPATH is correct. Not sure is this due to Shadow DOM. Please confirm

查看:32
本文介绍了获取“Nosuch 元素异常"在 Selenium 中虽然 XPATH 是正确的.不确定这是不是由于 Shadow DOM.请确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Selenium 自动化 Salesforce 应用程序并获取 NoSuchelementException 尽管 XPATH 对特定对象正确且有效.当我搜索问题时,可能是 Shadow DOM 的原因.

I am trying to automate Salesforce application Using Selenium and getting NoSuchelementException though XPATH is correct and valid for particular object. When i have searched the issue, it might be reason for Shadow DOM.

对于EX:

所以我写的 XAPTH,

So XAPTH i have written like,

driver.findElement(By.xpath("//input[@name='Name']")).sendKeys("Jams");

driver.findElement(By.xpath("//input[@id='input-299']")).sendKeys("Jams");

此 XPATH 也在控制台中突出显示.但是在自动化时它会抛出 nosuchelement 错误.

This XPATH is highlighting in Console as well. But while automating it throws nosuchelement error.

因此,在检查 ShadowDOM 选项时,我为 Name Object 获得了这样的选项.

So while checking for ShadowDOM option, am getting option like this for Name Object.

#shadow-root(user-agent)

暗影根 DIV

-- div 中没有提及.它只是打开和关闭标签.

-- nothing mentioned in div. it just open and closed tags.

如何实现自动化?

推荐答案

您可以检查您的 Dom 中是否有任何 iframe.只需在您的页面开发人员模式(F12)中执行 //iframe>元素选项卡 >搜索 (Ctrf+F) 区域.如果有,您将获得 iframe 的数量.

You can check if there are any iframes in your Dom. Simply do //iframe in your page developer mode(F12)> elements tab > search (Ctrf+F) area. If there are any, you will get the number of iframes.

现在,如果您的文本框在任何 iframe 中,请使用下面的代码首先进入特定的 iframe

Now if your textbox is in any of iframe use below code to go inside particular iframe first

driver.switch_to.frame("<name or Id of frame>")

然后出来框架使用如下:

then come out to frame use below:

driver.switch_to.parent_frame()

另外,如果问题与帧无关,请检查以下影子根相关问题:

Also, if the issue is not related to frames check below for shadow-root related issue:

您可以在下面检查 shadow-root 元素(问题是针对 Java,但您可以关联):

you can check below for shadow-root element ( Question is for Java, but you can co-relate):

如何交互使用 cssSelector 清除 Chrome 浏览器的浏览数据时使用 #shadow-root (open) 中的元素

这篇关于获取“Nosuch 元素异常"在 Selenium 中虽然 XPATH 是正确的.不确定这是不是由于 Shadow DOM.请确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆