我正在嘗試從表中抓取資料,但他們會為我提供空串列
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium import webdriver
driver= webdriver.Chrome('C:\Program Files (x86)\chromedriver.exe')
driver.get("https://www.fami-qs.org/certified-companies-6-0.html")
tabledata = driver.find_elements_by_xpath("//tbody/tr")
print(tabledata)
uj5u.com熱心網友回復:
由于<table>元素在 an 內,<iframe>因此您必須:
Induce WebDriverWait等待所需的框架可用并切換到它。
誘導WebDriverWait的
visibility_of_element_located()期望_Element,你可以使用以下的定位策略:使用CSS-SELECTOR:
driver.get("https://www.fami-qs.org/certified-companies-6-0.html") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='Inline Frame Example']"))) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@id='sites']//tbody"))).text)使用XPATH:
driver.get("https://www.fami-qs.org/certified-companies-6-0.html") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Inline Frame Example']"))) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#sites tbody"))).text)
注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC控制臺輸出:
FAM-1293 AmTech Ingredients Albert Lea UNITED STATES Valid 2020-10-08 2023-10-07 FAM-0841 3F FEED & FOOD S L Vizcolozano SPAIN Valid 2020-04-17 2023-04-16 FAM-1361 5N Plus Additives GmbH Eisenhüttenstadt GERMANY Valid 2020-10-01 2023-09-30 FAM-1301-01 A & V Corp. Limited Xiamen CHINA Valid 2020-09-09 2023-09-08 FAM-1146 A. E. Fischer-Chemie GmbH & Co. KG Wiesbaden GERMANY Valid 2020-06-05 2023-06-04 FAM-1589 A.M FOOD CHEMICAL CO LIMITED Jinan CHINA Valid 2020-01-07 2023-01-06 FAM-0613-01 A.W.P. S.r.l Crevalcore ITALY Valid 2020-02-27 2023-02-07 FAM-0867 AB AGRI POLSKA Sp. z o.o. Smigiel POLAND Valid 2020-08-03 2023-03-19 FAM-1510-02 AB Vista Marlborough UNITED KINGDOM Valid 2020-04-16 2023-04-15 FAM-1510-01 AB Vista * Rotterdam NETHERLANDS Valid 2020-04-16 2023-04-15
參考
您可以在以下位置找到一些相關討論:
- iframe下#document的處理方式
- 通過Selenium和python切換到iframe
- selenium.common.exceptions.NoSuchElementException:訊息:沒有這樣的元素:嘗試使用硒單擊“下一步”按鈕時無法定位元素
- python中的硒:NoSuchElementException:訊息:沒有這樣的元素:無法定位元素
uj5u.com熱心網友回復:
- 您嘗試訪問的表格元素位于 iframe 內。您必須先切換到該 iframe 才能訪問這些元素。
- 您應該提取表格文本以列印它們。
嘗試這個:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium import webdriver
driver= webdriver.Chrome('C:\Program Files (x86)\chromedriver.exe')
wait = WebDriverWait(driver, 20)
driver.get("https://www.fami-qs.org/certified-companies-6-0.html")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#inlineFrameExample")))
table = wait.until(EC.visibility_of_element_located((By.XPATH, "//tbody/tr")))
print(table.text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369465.html
