Getting “Nosuch element Exception” in Selenium Though XPATH is correct. Not sure is this due to Shadow DOM. Please confirm

时间秒杀一切 提交于 2021-01-28 05:55:00

问题


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.

For EX:

So XAPTH i have written like,

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

or

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

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

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

#shadow-root(user-agent)

Shadowroot DIV

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

How to automate this?


回答1:


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.

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:

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

How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector




回答2:


The website contents are mostly created now using javascript. You may have to wait for certain elements to load before doing anything to it.

https://seleniumbyexamples.github.io/wait



来源:https://stackoverflow.com/questions/63546935/getting-nosuch-element-exception-in-selenium-though-xpath-is-correct-not-sure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!