为什么不能用硒获得hkdrates表?

时间:2019-11-01 03:02:36

标签: python selenium iframe webdriver webdriverwait

我想获取带有硒的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")

现在,该网页包含使用硒的chrome驱动程序打开的hkdrates:
enter image description here

xpath以//*[@id="form-div"]/form/div/table[1]的形式显示在chrome中。
enter image description here

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

他们两个都什么也得不到,那如何获得hkdrates表呢?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

要使用Selenium获取 hkdrates 表,因为所需元素位于#include <iostream> #include <windows.h> int main() { while (1) { std::cout << "Hello World!\n"; Sleep(2000); } return 0; } 中,因此您必须:

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

    • 代码块:

      visibility_of_all_elements_located()
    • 控制台输出:

      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)
      
  

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

答案 2 :(得分:0)

您的表格位于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)