0

I am trying to switch my iframe to an iframe that's id changes every time. The source code is here:

<iframe class="card-fields-iframe ui-droppable" frameborder="0" id="card-fields-verification_value-f9yavo67out00000" name="card-fields-verification_value-f9yavo67out00000" scrolling="no" src="https://checkout.shopifycs.com/verification_value?identifier=b5fa1aecc18e4f685d7408c699c2ac5b&amp;location=https%3A%2F%2Fyeezysupply.com%2F17655971%2Fcheckouts%2Fb5fa1aecc18e4f685d7408c699c2ac5b%3F_ga%3D2.184035750.1808724638.1545611726-2044927600.1545611726&amp;dir=ltr" title="Field container for: CVV" style="height: 42px;" xpath="1"></iframe>

This is what I have tried. This has worked on the other iframes but not working on this one.

WebElement cVV = driver.findElement(By.xpath("//iframe[contains(@src,'verification')]"));
        driver.switchTo().frame(cVV);

I also tried with starts-with but still no luck. If there is a different approach that I am not aware of please let me know.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Also wanted to add that I tried it with different src like "https://checkout.shopifycs.com/verification_value", and Id like "card-fields-verification_value". – Naf Rahman Dec 24 '18 at 10:38
  • What have you tried with `start-with` ? Because `//iframe[starts-with(@id,'card-fields-verification_value')]` should work. – Nesku Dec 24 '18 at 10:49
  • @NafRahman Please don't edit the HTML based on which you have received well researched answers. Once you receive canonical answers changing the question can make all the answers invalid and may not be useful to future readers. If your requirement have changed feel free to raise a new question. StackOverflow contributors will be happy to help you out. For the time being I have reverted back the question to it's initial state. – undetected Selenium Dec 24 '18 at 11:12

4 Answers4

0

You can try by using title="Field container for: CVV"

xpath="//iframe[@title='Field container for: CVV']"
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

To switch to a dynamic <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • You can use the following solution:

    • Using CSS_SELECTOR:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.card-fields-iframe.ui-droppable[name^='card-fields-verification_value-'][src^='https://checkout.shopifycs.com/verification_value?identifier=']")));
      
    • Using XPATH:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='card-fields-iframe ui-droppable' and starts-with(@name, 'card-fields-verification_value-')][contains(@src, 'https://checkout.shopifycs.com/verification_value?identifier=')]")));
      

Here you can find a relevant discussion on Ways to deal with #document under iframe

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

If the HTML Source which is provided is as it is, I would suggest few things.

If you are directly switching to this frame from parent page, may try alternate attributes. Otherwise, make sure you are switching to DefaultContent first and then moving to this frame.

  1. Choose other attributes. May be Using Title for example. Hope this does not changes.
  2. I could see XPATH="1" as an attribute too. If that does not changes, you can simply say iframe[XPATH="1"]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
NiNa
  • 111
  • 1
  • 4
0

if you are bent on Identifying Iframe based on SRC value:

you can use following code using cssSelector wildcard * i.e. contains sub-string:

WebElement cVV = driver.findElement(By.cssSelector("iframe[src*='verification']"));
driver.switchTo().frame(cVV);
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46