一种让python-selenium的html在加载所有动态内容之后加载的方法?

2024-05-20 11:55:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过python selenium制作一个自动化文章编辑的脚本,但问题是:

打开页面后,页面本身显示良好,但我无法获得所需的元素。你知道吗

原来元素在某些元素中,selenium没有打开/读取这些元素。你知道吗

当我使用

iframe = driver.find_element_by_id('cafe_main').get_attribute('src')
driver.switch_to.frame(iframe)

它不起作用。出现“NoSuchFrameException:Message:no-suchframe”错误。你知道吗

  • 当我手动输入src时,会加载完全相同的页面。有一个JS只阻止iframe的那部分被加载。你知道吗

在那之后我尝试了这个:

try:
    element = WebDriverWait(driver, 10).until(
        expected_conditions.presence_of_element_located((By.XPATH, ".//*[@id='modifyFormBtn']/p/a"))
    )
finally:
    print(driver.page_source)

但仍然没有。。。。只是得到一个timeoutException和print(),在最后部分显示iframe没有被加载。你知道吗

所以。。。有没有别的办法让这个东西装上子弹?你知道吗

编辑: 我尝试在driver.switch\至.frame(iframe),我得到了这个错误。你知道吗

Message: unknown error: Element is not clickable at point (784, 864). Other element would receive the click: ... (Session info: chrome=58.0.3029.110) (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.14393 x86_64)

http://cafe.naver.com/twcenter/22498这是我正在使用的链接,div标签中应该有文章, id="main-area" area, but only shows in the selenium

编辑2:

我的完整代码:

import os
from selenium import webdriver

chromePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'chromedriver')
# setting up browser. this is Chrome-based.
driver = webdriver.Chrome(chromePath)

# tha address. for testing it out
driver.get('http://cafe.naver.com/twcenter/22498')

driver.implicitly_wait(15)  # tried waiting if dynamic script gets loaded. it didn't.

# opening the frame. 
iframe = driver.find_element_by_id('cafe_main')  
print(iframe)
driver.switch_to.frame(iframe)

# an xpath going to a button opening forum post lists. 
xpath = ".//*[@id='main-area']/div[3]/div/div[2]/p/a"


try:
    # not working at the moment as iframe is not being dynamically updated in selenium. 
    driver.find_element_by_xpath(xpath).click()
except Exception as e:
    print(e)

Tags: thedivid元素编辑cafeosmain