如何通过python selenium加载页面?

问题描述 投票:0回答:1

我可以用任何浏览器查看页面,当我用Selenium打开它时只能看到空白,我发现有如下javaScript,我想我需要运行这段代码,下一步我该怎么办?

<body>
    <script>
        !function(){
            var e=document.createElement(“iframe”);
            function n(){
                var n=e.contentDocument||e.contentWindow.document;
                if(n){
                    var t=n.createElement(“script”);
                    t.nonce=“”,t.innerHTML=“window[‘__CF$cv$params’]={r:‘7e88f63d224cc37d’,m:‘JpppFqs3NGPEA327hy7XHbhTbKXvZxzVh_Th60Z2NO4-1689664824-0-AdsfsWHmOM/vJvQfOZX4DHS1zxskac6BpgnnirFZJp3k’};
                    _cpo=document.createElement(‘script’);
                    _cpo.nonce=‘’,_cpo.src=‘/cdn-cgi/challenge-platform/scripts/invisible.js’,document.getElementsByTagName(‘head’)[0].appendChild(_cpo);“,n.getElementsByTagName(“head”)[0].appendChild(t)}}if(e.height=1,e.width=1,e.style.position=“absolute”,e.style.top=0,e.style.left=0,e.style.border=“none”,e.style.visibility=“hidden”,document.body.appendChild(e),“loading”!==document.readyState)n();
                else if(window.addEventListener)
                    document.addEventListener(“DOMContentLoaded”,n);
                else{
                    var t=document.onreadystatechange||function(){};
                    document.onreadystatechange=function(e){
                        t(e),“loading”!==document.readyState&&(document.onreadystatechange=t,n())
                    }
            }
        }();
    </script>
    ...
</body>
javascript python selenium-webdriver
1个回答
0
投票

如果 html 元素加载了 javascript,则无需运行 javascript。 相反,您可以通过编程方式等待整个页面加载完毕。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

dr = webdriver.Chrome()
dr.get('www.website.com')

# wait for 10 secs and get element
ele = wait(dr,10).until(EC.presence_of_element_located((By.CLASS_NAME,"<class_name>")))

# if you want to interact with any element
ele.click()

dr.quit()

如果在某些情况下,你想执行 javascript,即使使用 selenium 也是可能的

from selenium import webdriver
dr = webdriver.Chrome()

js = '''
<script>
    !function(){
        var e=document.createElement(“iframe”);
        function n(){
            var n=e.contentDocument||e.contentWindow.document;
            if(n){
                var t=n.createElement(“script”);
                t.nonce=“”,t.innerHTML=“window[‘__CF$cv$params’]={r:‘7e88f63d224cc37d’,m:‘JpppFqs3NGPEA327hy7XHbhTbKXvZxzVh_Th60Z2NO4-1689664824-0-AdsfsWHmOM/vJvQfOZX4DHS1zxskac6BpgnnirFZJp3k’};
                _cpo=document.createElement(‘script’);
                _cpo.nonce=‘’,_cpo.src=‘/cdn-cgi/challenge-platform/scripts/invisible.js’,document.getElementsByTagName(‘head’)[0].appendChild(_cpo);“,n.getElementsByTagName(“head”)[0].appendChild(t)}}       if(e.height=1,e.width=1,e.style.position=“absolute”,e.style.top=0,e.style.left=0,e.style.border=“none”,e.style.visibility=“hidden”,document.body.appendChild(e),“loading”!==document.readyState)n();
            else if(window.addEventListener)
                document.addEventListener(“DOMContentLoaded”,n);
            else{
                var t=document.onreadystatechange||function(){};
                document.onreadystatechange=function(e){
                    t(e),“loading”!==document.readyState&&(document.onreadystatechange=t,n())
                }
        }
    }();
    </script>
'''
dr.execute_script(js)
© www.soinside.com 2019 - 2024. All rights reserved.