使用Selenium和phantomJS和python在iframe中获取完全生成的DOM元素

问题描述

好,我卡住了。我使用selenium和PhantomJS制作了一些网络抓取python脚本。我正在处理的页面在我的Web驱动程序无法运行的iframe文档中包含了我想要的数据。

export

我想获取export -n中的<main Page Heads etc> <blah> <iframe 1 src="src1" ... etc etc> #document <tag> <tag> <iframe2 src="src2"><iframe2> <iframe1> <blah> <end of webpage DOM> 。我尝试通过Webdriver运行src URL,但我得到的只是原始页面html,而不是已加载的网页元素,iframe2必须由src1内部的某些脚本创建,但是我无法让我的网络驱动程序运行脚本。

有什么想法吗?

这是我在网页上运行javascript以获取编译后的页面DOM的目的:

iframe2

解决方法

您无法获得完整的page_source。如果是iframe,则应使用以下命令:switch_to.frame(iframe_element),以便可以在

内获取元素
n = int(input())

while n != 6:
    if n == 1:
        a = int(input())
        b = int(input())
        print(a + b)
    elif n == 2:
        a = int(input())
        b = int(input())
        print(a - b)
    elif n == 3:
        a = int(input())
        b = int(input())
        print(a * b)
    elif n == 4:
        a = int(input())
        b = int(input())
        print(a // b)
    elif n == 5:
        a = int(input())
        b = int(input())
        print(a % b)
else:
    print("Invalid Operation")

再次返回,您可以使用以下命令获取iframe的外部元素;

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC

self.driver = webdriver.PhantomJS()
self.driver.get(url)



WebDriverWait(self._driver,50).until(
            EC.presence_of_all_elements_located
            ((By.XPATH,'//iframe[@id="iframegame"]'))
        )

iframe_element = self.driver.find_element_by_xpath('//iframe[@id="iframegame"]')

self.driver.switch_to.frame(iframe_element)

tag = self.driver.find_element_by_xpath('//tag')

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...