I am trying to locate an iframe by partial id. For this method, I used:
driver.switchTo().frame(driver.findElement(By.cssSelector(\"iframe[id*=\'card-fields-number\']
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:
<iframes>.
<iframe> is in play src attribute of the <iframe> tag plays a vital role.
<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')]")));
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();