Python 硒网刮削iframe
我想读取碳粉值在我办公室各种打印机的网页上 问题是这个页面是由几个框架组成的,其中有剩余的墨粉,是用js写的,即使使用selenium我也看不懂 这是我的代码:Python 硒网刮削iframe,python,selenium,xpath,iframe,css-selectors,Python,Selenium,Xpath,Iframe,Css Selectors,我想读取碳粉值在我办公室各种打印机的网页上 问题是这个页面是由几个框架组成的,其中有剩余的墨粉,是用js写的,即使使用selenium我也看不懂 这是我的代码: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.expected_conditions import ( presence_of_element_located
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import (
presence_of_element_located)
from selenium.webdriver.support.wait import WebDriverWait
def get_comment_count(driver, url):
driver.get(url)
wait = WebDriverWait(driver, 3)
e = driver.find_elements_by_xpath("/html/frameset/frame")
driver.switch_to_frame(e[0])
toner_iframe = driver.find_elements_by_xpath('//*[@id="contain"]')
# iframe_url = toner_iframe.get_attribute('src')
#driver.switch_to_frame(toner_iframe)
driver.switch_to.frame(toner_iframe)
print(toner_iframe)
url = "https://pritner_web_page"
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(options=options)
get_comment_count(driver,url)
我也试过
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(options=options)
driver.get("http://printer_web_page")
WebDriverWait(driver,5).until(EC.frame_to_be_available_and_switch_to_it((By.ID,'wlmframe')))
WebDriverWait(driver,5).until(EC.frame_to_be_available_and_switch_to_it((By.ID,'toner')))
page_source=driver.page_source
print(page_source)
这是页面的DOM检查器。各种帧都是动态的,用js编写,如下所示:
我编写的代码只是尝试进入框架的几种不同尝试之一,但没有任何效果元素位于嵌套的
/
元素中,因此您必须:
- 诱导父帧可用并切换到它
- 诱导子帧可用并切换到它
- 使所需元素可单击
- 您可以使用以下任一选项:
- 使用
:CSS\u选择器
driver.get("http://printer_web_page") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='wlmframe']"))) WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#toner[name='toner']")))
- 使用
:XPATH
driver.get("http://printer_web_page") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@name='wlmframe']"))) WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='toner' and @name='toner']")))
- 使用
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考文献 您可以在以下内容中找到一些相关讨论:
toner\u iframe=driver。通过xpath('/*[@id=“toner”]')查找元素\u
,然后使用driver切换到它。切换到.frame(toner\u iframe)
是的,我尝试过,但这是结果。。。selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:找不到元素:{“method”:“xpath”,“selector”:“/*[@id=“toner”]”}我也尝试过这个解决方案,但出现了错误:回溯(最近一次调用):文件“c:\Users\j972537\Desktop\Venv_Scraping\Scraping\Scraping_toner_3.py”,第15行,在WebDriverWait中(驱动程序,20).until(EC.frame\u to\u be\u available\u and\u switch\u to \u it((By.XPATH,//iframe[@name='wlmframe']))文件“C:\Users\j972537\Desktop\Venv\u Scraping\Scraping\lib\site packages\selenium\webdriver\support\wait.py”,第80行,直到引发TimeoutException(消息、屏幕、堆栈跟踪)selenium.common.exceptions.TimeoutException:Message:css选择器的错误与WebDriverWait(driver,20)相同。直到(EC.frame\u to\u be\u available\u和\u switch\u to\u it((By.css\u选择器,“iframe[name='wlmframe']”)文件“C:\Users\j972537\Desktop\Venv\u Scraping\Scraping\lib\site packages\selenium\webdriver\support\wait.py”,第80行,在until raise TimeoutException(message,screen,stacktrace)selenium.common.exceptions.TimeoutException:message:@Gian76代码中有一个bug,我现在已经修复了。请重新测试并告诉我结果。