-2

I've looked at similar questions and I think what I'm trying to find is inside an iframe. But I don't know how to switch to the frame or search for the frame. Can someone help please?

Actually I don't know for sure if it is an iframe problem. If it is:

Need help with 1) Finding the iframe 2) then switching to that iframe so i can input text into element either by name or id as below.

If it isn't, what's the issue?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
main='http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx'


driver=webdriver.Chrome(path)
driver.get(main)
elem = driver.find_element_by_id("ct100_txt_stock_code")
elem = driver.find_element_by_name("ct100$txt_stock_code")
jason
  • 3,811
  • 18
  • 92
  • 147
  • it took me literally about 10 seconds to find this: https://stackoverflow.com/q/44834358/7432 – Bryan Oakley Jul 29 '18 at 21:50
  • @BryanOakley ok... ??? so how does that help me... do you see any `iframe` tags in my html? – jason Jul 29 '18 at 22:00
  • You wrote _"I don't know how to switch to the frame"_ and the title of your question mentions how to switch to an iframe, and that question shows you how to switch to an iframe. It's not really clear what you need help with. Do you need help with finding the frame element, or switching to it, or something else? – Bryan Oakley Jul 29 '18 at 22:04
  • @BryanOakley How about now? clear? – jason Jul 29 '18 at 22:31

2 Answers2

2

You can use this working code snippet:

driver.get("http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx")

stock_code = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "ctl00_txt_stock_code")))
stock_code.click()
stock_code.send_keys("12345")
time.sleep(5) # this is only to see the result

Note: you have to add some imports:

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

The idea is to use WebDriverWait, which will wait at least 10 seconds until element will be clickable and only then clicks on it. There is no iframes, so the problem is that your script is too fast and tries to click on the element, which is not currently in the DOM. WebDriverWait fixes this.

PS: I would recommend to have a look on the documentation to get more information.

Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
2

As per the url http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx to send a character sequence to the element with text as Stock Code you need to induce WebDriverWait for the desired element to be clickable and you can use the following code block:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    main='http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx'
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get(main)
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='arial12black' and @id='ctl00_txt_stock_code']"))).send_keys("51584821")
    
  • Browser Snapshot:

hknews

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