1

I am trying to perform the payment by credit card using dummy data using Selenium. The problem I am having is that #document doesn't show when I run my selenium code when i do it manually it only shows after I refresh the page but when I try to do the same thing in selenium even though the page refreshes the #document doesn't show.

Html when done by selenium :

<body class="script-body">
<div class="container"></div>
<script type="text/javascript"></script>
<iframe src="https://libs.hipay.com/hostedfields/index.html#type=card&amp;element=controller&amp;instanceId=200803&amp;referrer=https://stage-secure-gateway.hipay-tpp.com" frameborder="0" allowtransparency="true" scrolling="no" width="0" height="0" name="hipay-controller-200803" title="hipay-controller-200803" id="hipay-controller-200803" data-hipay-id="hostedfield-iframe"></iframe>
</body>

When i do it manualy :

When done Manualy

My code :

WebElement ClkPaiementByCard = driver.findElement(By.id("payed-cart"));
js.executeScript("arguments[0].click();", ClkPaiementByCard);
driver.findElement(By.className("script-body")).sendKeys(Keys.F5);
WebElement CardNumber = driver.findElement(By.xpath(
        "//div[@id=\"root\"]/div/div/form/div/div/input[@name=\"cardnumber\"]"));
CardNumber.clear();
CardNumber.sendKeys("4111 1111 1111 1111");
WebElement ExpDate = driver.findElement(By.xpath(
        "//div[@id=\"root\"]/div/div/input[@name=\"cc-exp\"]"));
ExpDate.clear();
ExpDate.sendKeys("05/24");
WebElement CVC = driver.findElement(By.xpath(
        "//div[@id=\"root\"]/div/div/input[@name=\"cvc\"]"));
CVC.clear();
CVC.sendKeys("123");            
WebElement SubmitPaiement = driver.findElement(By.id("submit-button"));
js.executeScript("arguments[0].click();", SubmitPaiement);

I tried to do the switchto().frame() but it never works I tried to perform an action that refreshes the page but it doesn't work neither can someone explain to me what I am doing wrong.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

The iframe is clearly visible. You need to switch to the iframe first to access the elements as follows:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[starts-with(@id, 'hipay-controller') and @data-hipay-id='hostedfield-iframe']")));

preferably using WebDriverWait as follows:

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@id, 'hipay-controller') and @data-hipay-id='hostedfield-iframe']")));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Sorry my main problem is that i dont understand what the switch will do if you can explain is it going to acces the #document even tough it's not showing in the HTML ? – Tester testing Nov 12 '21 at 13:34
  • i get this error org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id="root"]/div/div/form/div/div/input[@name="cardnumber"]"} and when i put the xpath in element console it s not found since the #document isn't showing once i refresh it shows – Tester testing Nov 12 '21 at 13:39
  • _`org.openqa.selenium.NoSuchElementException`_ sounds to be a new error all together when you try interacting with the elements within the `iframe`. You may like to raise a new question with all the relevant details. – undetected Selenium Nov 12 '21 at 13:45
  • as i said in this question the reason for this error is that the #document isn't present when you acces the page for the first time so even though he switches into the frame he has nothing to select since there is nothing there my last question where i put this was closed and all the related questions non had an answer for my probleme. – Tester testing Nov 12 '21 at 13:49
  • Reread your question of this discussion and my answer again. Had `#document` not been present `driver.switchTo().frame()` wouldn't have been successful. There are certain ways to counter _`org.openqa.selenium.NoSuchElementException`_ which is beyond the scope of this question. – undetected Selenium Nov 12 '21 at 13:58
  • i cant put a new question sadly is there a solution to no such element exception ? – Tester testing Nov 12 '21 at 14:31