1

Im trying to enter the username field on a website and I realise it is inside an iframe on the page. The iframe html code and input field html code are as follows:

<iframe class="_2_i9tg _2_i9tg" src="https://gateway-members.bet365.com/v1/auth/oauth/authorise?client_id=BDE2D5E4-127D-48D4-A963-BB49DC126BF7&amp;scope=sports%20members&amp;response_type=code&amp;redirect_uri=https://secure.oddschecker.com/mapp/b3/callback&amp;state=dXNlcklEOmExNWM4OTUxLTlkY2EtNDQ5OC1iNDc3LWUxNzVhMDdjODdlNVNFUGRldmljZVR5cGU6d2luZG93c1NFUGFwaVJlcHViOmFwaV9PQ1NFUGRldmljZUlkOmExNWM4OTUxLTlkY2EtNDQ5OC1iNDc3LWUxNzVhMDdjODdlNQ"></iframe>

<input autocomplete="off" class="inputtextbox" id="Username" name="Username" placeholder="Username" type="text" value="">

I have attempted to switch to the iframe and input text multiple ways such as

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@class='_2_i9tg _2_i9tg']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id = 'Username'][@class = 'inputtextbox']"))).send_keys("Username")

and

my_iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//iframe[@class='_2_i9tg _2_i9tg']")))
driver.switch_to.frame(my_iframe)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//@id = 'Username'][@class = 'inputtextbox']"))).send_keys("Username")

When I try to run this code i get an error saying:

Browsing context has been discarded

When I don't try switch frame I cannot locate the input field element. What am I missing?

  • What is the website you are trying to access? Your code looks more or less correct – goalie1998 Feb 01 '21 at 01:56
  • The website is https://www.oddschecker.com. The problem occurs when I am trying to log in to a bookmakerds after adding a selection to betslip – Eglantine46 Feb 01 '21 at 02:42
  • It's hard to say without seeing your exact sequence of events and clicks, but when I press login on the website, the login form does not load in a new iframe. – goalie1998 Feb 01 '21 at 02:49
  • Sorry let me try to clarify, I'm not attempting to log in to the oddschecker website. If you go to the site and select something and add it to your betslip, you get the option to log in to the bookmaker of your choice eg bet365. This log in form then appears and when I inspect it is in an ifram, I will add screenshot to my original post. – Eglantine46 Feb 01 '21 at 02:58
  • Ah, I see. Unfortunately the bet slip isn't working for me, so I can't troubleshoot. Sorry. – goalie1998 Feb 01 '21 at 03:08

2 Answers2

0

There is a typo in second try. If it doesn't fix it can you provide html code?

//*[@id = 'Username'][@class = 'inputtextbox']
ble
  • 287
  • 1
  • 5
  • This didnt fix, how should I provide html code? Are the lines in my post not sufficient, i'm unsure how to copy the full html code as it only copys the html for the specific elements when I try. – Eglantine46 Feb 01 '21 at 02:44
  • Nevermind, I can see you attached pictures and everything looks good from the code as far as see. Have you tried adding `my_iframe = driver.find_element_by_xpath("//iframe[@class='_2_i9tg _2_i9tg']")` before `driver.switch_to.frame(my_iframe)` – ble Feb 01 '21 at 19:13
0

As the element with the element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://gateway-members.bet365.com/v1/auth/oauth/authorise?client_id']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.inputtextbox#Username[name='Username']"))).send_keys("Eglantine46")
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://gateway-members.bet365.com/v1/auth/oauth/authorise?client_id')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='inputtextbox' and @id='Username'][@name='Username']"))).send_keys("Eglantine46")
      
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for response, Ive tried multiple ways but this code still returns the error 'Browsing context has been discarded' – Eglantine46 Feb 01 '21 at 13:46