【问题标题】:Why can't get the hkdrates table with selenium?为什么不能用硒获得 hkdrates 表?
【发布时间】:2019-11-01 03:02:36
【问题描述】:

我想用 selenium 获取 hkdrates 表:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
browser = webdriver.Chrome(options=chrome_options,executable_path='/usr/bin/chromedriver')
browser.get("https://www.bochk.com/en/investment/rates/hkdrates.html")

现在网页包含使用 selenium 的 chrome 驱动程序打开的 hkdrates:

xpath 是 //*[@id="form-div"]/form/div/table[1],在 chrome 中显示。

browser.find_elements_by_xpath('//*[@id="form-div"]/form/div/table')
[]
browser.find_elements_by_xpath('.//table') 
[]

他们都一无所获,那么如何获得hkdrates表呢?

【问题讨论】:

  • 首先使用 print(browser.page_source) 检查结果...您会注意到标签表没有任何内容,因为该表位于 iframe 内

标签: python selenium iframe webdriver webdriverwait


【解决方案1】:

你的意思是它在 iframe 里面的表,你需要先切换。你可以使用一个方法:

.frame_to_be_available_and_switch_to_it

iframe 有一个id: iframe

对于您的hkdrates table,您可以使用css selector: .form_table.import-data.second-right

browser.get('https://www.bochk.com/en/investment/rates/hkdrates.html')

WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'iframe')))
hkdrates_tbl = browser.find_element_by_css_selector('.form_table.import-data.second-right')
print(hkdrates_tbl.text)
browser.quit()

导入后:

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

【讨论】:

    【解决方案2】:

    您的表格位于 iframe 内,请更改 https://www.bochk.com/whk/rates/exchangeRatesHKD/exchangeRatesHKD-input.action?lang=en 的网址

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    browser = webdriver.Chrome(options=chrome_options,executable_path='/usr/bin/chromedriver')
    browser.get("https://www.bochk.com/whk/rates/exchangeRatesHKD/exchangeRatesHKD-input.action?lang=en")
    table=browser.find_elements_by_xpath('//*[@id="form-div"]/form/div/table/tbody/tr')
    for x in table:
        print(x.text)
    

    【讨论】:

    【解决方案3】:

    要使用 Selenium 获取 hkdrates 表,因为所需元素位于 <iframe> 内,因此您必须:

    • 为所需的frame_to_be_available_and_switch_to_it() 诱导 WebDriverWait
    • 为所需的visibility_of_all_elements_located() 诱导 WebDriverWait
    • 您可以使用以下解决方案:

      • 代码块:

        from selenium import webdriver
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.support import expected_conditions as EC
        
        options = webdriver.ChromeOptions()
        options.add_argument("start-maximized")
        driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
        driver.get("https://www.bochk.com/en/investment/rates/hkdrates.html")
        WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='iframe' and starts-with(@src, '/whk/rates/exchangeRatesHKD/exchangeRatesHKD-input')]")))
        WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "table.form_table.import-data.second-right tbody tr")))
        print(driver.find_element_by_css_selector("table.form_table.import-data.second-right tbody").text)
        
      • 控制台输出:

        Currency Buy Sell
        CNY 90.290000 89.360000
        CNH 90.380000 89.400000
        USD 782.250000 785.250000
        GBP 1,008.800000 1,020.400000
        JPY 721.000000 730.200000
        AUD 536.950000 543.800000
        NZD 500.200000 506.650000
        CAD 591.950000 599.100000
        EUR 868.400000 878.400000
        CHF 790.350000 797.850000
        DKK 116.150000 117.750000
        NOK 85.000000 86.400000
        SEK 80.650000 82.150000
        SGD 575.200000 579.450000
        THB 25.550000 26.550000
        BND 575.200000 579.450000
        ZAR 51.000000 52.950000
        

    在这里你可以找到Ways to deal with #document under iframe的相关讨论

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2012-09-08
      相关资源
      最近更新 更多