为什么不能用 selenium 得到 hkdrates table?

Why can't get the hkdrates table with selenium?

我想用 selenium 获取 hkdrates table:

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') 
[]

他们俩什么都得不到,那怎么得到hkdratestable?

您的 table 在 iframe 中,将 url 更改为 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)

table 你的意思是它在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

要使用 获得 hkdrates table,因为所需的元素在 <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
      

Here you can find a relevant discussion on