【发布时间】:2021-05-19 03:12:41
【问题描述】:
我想在我办公室的各种打印机的网页上读取碳粉值。
问题是页面是由几帧组成的,其中有剩余墨粉的那一帧是用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.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 Inspector。各种帧都是动态的,用js写的如下:
我编写的代码只是获取框架的几种不同尝试之一,但无济于事
【问题讨论】:
-
我看到您正在搜索 iframe 元素,而不是元素。您是否尝试过类似的方法:
toner_iframe = driver.find_element_by_xpath('//*[@id=“toner”]’),然后使用driver.switch_to.frame(toner_iframe)切换到它 -
是的,我试过了,但这是结果... selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“xpath”,选择器":"//*[@id="toner"]"}
标签: python selenium xpath iframe css-selectors