如何通过Python Selenium加载页面?

0 投票
1 回答
25 浏览
提问于 2025-04-14 15:20

我可以用任何浏览器打开这个页面,但用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>

1 个回答

0

如果网页上的元素是通过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)

撰写回答