【问题标题】:Evade detection of selenium automation逃避检测硒自动化
【发布时间】:2021-05-17 19:26:18
【问题描述】:

为了测试我的技能,我正在编写一个 Python 软件,它应该转到网页 https://www.solebox.com/de_DE,选择产品并将名称、标签和价格保存在 .txt 文件中(或将其转换为未来)使用 Selenium 库。问题是该站点检测到我正在使用自动软件,并且不允许我访问这些产品。我已经尝试过使用undetected_chromedriver 库,但它没有用。有人知道工作方法吗?谢谢。

更多信息:操作系统:Windows 10,Chrome 版本:88.0.4324.150 64 位,Python 版本:3.9.1,编写软件:Visual Studio Code

【问题讨论】:

    标签: python selenium bots


    【解决方案1】:
    options=webdriver.ChromeOptions()
    
    options.add_experimental_option(
        "excludeSwitches", ['enable-automation'])
        
    
    driver = webdriver.Chrome(options=options)
    
    
    driver.get("https://www.solebox.com/de_DE")
    

    只需排除将禁用 navigator.webdriver 对象的自动化开关

    https://developer.mozilla.org/en-US/docs/Web/API/Navigator/webdriver

    进一步调查发现,该网站还观察到屏幕导航过快并引发自动化错误。您可以通过单击产品并单击退格键并继续此过程来验证这一点

    【讨论】:

    • 这个方法我已经试过了,还是不行。我也尝试按照许多教程中的说明更改屏幕分辨率,但没有办法。
    • 我试过了,它可以解决您遇到的错误
    • 尝试添加 options.add_argument('--disable-blink-features=AutomationControlled')
    • 它仍然不起作用,我没有从控制台收到错误但我仍然无法访问网页。消息如下:“访问此页面已被拒绝,因为我们认为您正在使用自动化工具浏览网站。这可能是由于以下原因: ●Javascript 被禁用或被扩展程序阻止(广告拦截器例如) ●您的浏览器不支持cookies 请确保您的浏览器启用了Javascript和cookies,并且您没有阻止它们加载。参考ID:1c2b8b30-6f73-11eb-aae2-8f373c0a0244“
    • 你试过我提到的吗,即使你点击页面返回,然后连续点击任何元素,你也会得到错误
    【解决方案2】:

    有多种方法可以规避Selenium 自动化的检测。


    使用--disable-blink-features=AutomationControlled

    代码块:

    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('--disable-blink-features=AutomationControlled')
    
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://www.solebox.com/de_DE')
    print(driver.page_source)
    

    控制台输出:

    <!-- =============== This snippet of JavaScript handles fetching the dynamic recommendations from the remote recommendations server
    and then makes a call to render the configured template with the returned recommended products: ================= -->
    
    <script>
    (function(){
    // window.CQuotient is provided on the page by the Analytics code:
    var cq = window.CQuotient;
    if (cq && ('function' == typeof cq.getCQUserId)
    && ('function' == typeof cq.getCQCookieId)
    && ('function' == typeof cq.getCQHashedEmail)
    && ('function' == typeof cq.getCQHashedLogin)) {
    var recommender = '[[&quot;Homepage_Topseller&quot;]]';
    // cleaning up the leading/trailing brackets and quotes:
    recommender=recommender.slice(8, recommender.length-8);
    var separator = '|||';
    .
    </script>
    <script type="text/javascript">//<!--
    /* <![CDATA[ (viewProduct-active_data.js) */
    dw.ac._capture({id: "01900289", type: "recommendation"});
    /* ]]> */
    // -->
    </script>
    .
    <script type="text/javascript" id="" src="//static.criteo.net/js/ld/ld.js"></script>
    <script type="text/javascript" id="">window.criteo_q=window.criteo_q||[];window.criteo_q.push({event:"setAccount",account:google_tag_manager["GTM-M9TMD24"].macro(24)},{event:"setEmail",email:""},{event:"setSiteType",type:"d"},{event:"viewHome"});</script><div id="criteo-tags-div" style="display: none;"><iframe src="https://gum.criteo.com/syncframe?topUrl=www.solebox.com#{&quot;bundle&quot;:{&quot;origin&quot;:0,&quot;value&quot;:null},&quot;cw&quot;:true,&quot;lwid&quot;:{&quot;origin&quot;:0,&quot;value&quot;:null},&quot;optout&quot;:{&quot;origin&quot;:0,&quot;value&quot;:null},&quot;origin&quot;:&quot;onetag&quot;,&quot;pm&quot;:0,&quot;sid&quot;:{&quot;origin&quot;:0,&quot;value&quot;:null},&quot;tld&quot;:&quot;solebox.com&quot;,&quot;topUrl&quot;:&quot;www.solebox.com&quot;,&quot;uid&quot;:null,&quot;version&quot;:&quot;5_6_2&quot;}" id="criteo-syncframe" width="0" height="0" frameborder="0" style="border-width:0px; margin:0px; display:none" title="Criteo GUM iframe"></iframe></div></body></html>
    

    您可以在Selenium can't open a second page找到相关的详细讨论


    使用 undetected_chromedriver

    代码块:

    import undetected_chromedriver as uc
    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    driver = uc.Chrome(options=options)
    driver.get("https://www.solebox.com/de_DE")
    print(driver.page_source)
    

    控制台输出:

    .
    .
    <script type="text/javascript" id="">!function(b,e,f,g,a,c,d){b.fbq||(a=b.fbq=function(){a.callMethod?a.callMethod.apply(a,arguments):a.queue.push(arguments)},b._fbq||(b._fbq=a),a.push=a,a.loaded=!0,a.version="2.0",a.queue=[],c=e.createElement(f),c.async=!0,c.src=g,d=e.getElementsByTagName(f)[0],d.parentNode.insertBefore(c,d))}(window,document,"script","https://connect.facebook.net/en_US/fbevents.js");fbq("init",google_tag_manager["GTM-M9TMD24"].macro(19));fbq("track","PageView");</script>
    <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=238536633211197&amp;ev=PageView&amp;noscript=1"></noscript>
    
    <script type="text/javascript" id="" src="//static.criteo.net/js/ld/ld.js"></script></body></html>
    

    您可以在Undetected Chromedriver not loading correctly找到相关的详细讨论

    【讨论】:

    • 我尝试了,但仍然无法访问该页面。您可以在 PDHide 的答案的 cmets 中找到错误消息
    • @Maxwell 此答案中提到了两种方法。您尝试了哪种方法?你看到什么错误?使用您的代码试验和最近的错误更新问题。
    • 这两种方法我都试过很多次了,都没有奏效。我再次尝试了 undetected_chromedriver 方法,奇怪的是它现在可以工作了。我想我已经解决了这个问题,如果它再次停止工作,我会通知你。谢谢回复。 (ps:undetected_chromedriver这个方法好像不太稳定,大概70%的时候都能正常工作,有问题我会通知你的)
    • @Maxwell 当然,如果你被困在任何地方,请告诉我。
    • 又来了,现在软件自动点击某物时出现问题
    猜你喜欢
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    相关资源
    最近更新 更多