org.openqa.selenium.NoSuchElementException: Returned node (null) was not a DOM element when trying to locate card-fields-iframe by CssSelector

大兔子大兔子 提交于 2019-11-29 15:32:46

This error message...

org.openqa.selenium.NoSuchElementException: Returned node (null) was not a DOM element

...implies that there was no such element found as the returned node was null or was not a DOM element.

This is still a open issue with htmlunit-driver team.

However there are certain things which you need to take care as follows:

  • First and foremost, all the modern browsers come with built-in support for JavaScript.
  • HtmlUnitDriver is a WebDriver compatible driver for HtmlUnit headless browser. It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating Chrome, Firefox or Internet Explorer depending on the configuration used. So ideally while working with HtmlUnitDriver, JavaScript must be enabled, else HtmlUnitDriver may not ne able to detect the JavaScript based elements.
  • The element seems to be a credit card field and historically Credit Card Number, etc resides within <iframes>.
  • Whenever an <iframe> is in play src attribute of the <iframe> tag plays a vital role.
  • As per best practices while switching <iframe> you need to induce WebDriverWait for the desired frame to be available and switch to it.

So you can use either of the following solutions:

  • cssSelector:

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.card-fields-iframe[id^='card-fields-number-'][src*='shopifycs']")));
    
  • xpath:

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='card-fields-iframe' and starts-with(@id,'card-fields-number-')][contains(@src, 'shopifycs')]")));
    

Update

See the snapshot of the CssSelector which identifies the element perfecto as per the HTML you have provided:

Just try with frame indexing "//iframe[index]", where index integer.

e.g. xpath : //iframe[1]

Frame id may change dynamically but in a few application structure remains same so indexing solves the problem.

Please let me know if it solves the issue.

As the iframe has tag iframe, you can switch to the iframe using the tagname like:
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
And if you want to again switch to the default content, then you can use driver.switchTo().defaultContent();

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